0

I'm trying to write a query that will return ...

  1. Each distinct user id in the accesslog table.

  2. A count of how many distinct ip addresses each userid has accessed the site with, also from the accesslog table.

  3. That userid's name, email address, and company name, from the users table.

I've got 1 and 2, but I'm not sure how to get 3. I suck at joins, I admit. :(

Here's the query I have now:

SELECT DISTINCT userid, COUNT(ipaddress) AS c FROM accesslog WHERE url LIKE 'www.example.com%' and userid != '' GROUP BY userid ORDER BY c desc

How would I JOIN in the other pieces of data that I want?

1
  • Ah cr@p... just remembered that my users table and accesslog table are in different databases, and I don't have access to a root mysql user. So I can't do this query after all.. :( Commented Aug 25, 2009 at 23:58

1 Answer 1

4

You'll need to join to users table and add all its columns to GROUP BY:

SELECT a.userid, u.name, u.email, u.company, COUNT(a.ipaddress) AS c
  FROM accesslog a, USERS u
 WHERE a.userid = u.userid
   AND a.url LIKE 'www.example.com%'
   AND a.userid != ''
GROUP BY a.userid
ORDER BY c desc
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think you'll need 'DISTINCT' as the 'group by' should take care of that.
thanks for your answer. it would have worked if my databases weren't so screwy.
@BoltBait - you're right; I've just copied / edited Ian's query and forgot to remove it. Will edit.

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.