0

I've got 2 tables:

----Users-----

  • id (int)
  • username (varchar)

----Log-----

  • id (int)
  • user_id (int)
  • logged (datetime)

Every time the user logs in, a record is inserted into the Log table. I would like to create a statement that will output:

EXAMPLE:

Jason -> 96
Mary -> 54
Jack -> 53

I've been pulling my hair out with this SQL statement required to do this. Any insight?

1
  • 4
    More info please. What is "96"? How many times should each user appear in the output? Commented Jan 5, 2011 at 18:30

2 Answers 2

1
Select Users.username , Count (Log.user_id)
From Users, Log
WHERE Users.id=Log.user_id
Group by log.user_id
Sign up to request clarification or add additional context in comments.

2 Comments

You are welcome . you can vote the question up and mark it as answer if it helps :)
dangerous group by, in standard SQL you should have the Users.username in the GROUP BY, this extended group by syntax on MySQl is dangerous, should be avoided for beginners. I would even add the User.id in the Group by and select to avoid duplicate names if username is not unique.
0

I'm going to make an assumption from the small amount of info given. I think your best bet would be something like:

select u.username, ' -> ', l.user_id from Users u, Log l where l.user_id = u.id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.