Posted: Wed Feb 06, 2008 2:25 am Post subject: Problem with Count when selecting from multiple tables.
Hello. I currently am working on developing an application. For reference, the related mysql tables look like this (slimmed down to applicable fields):
`users`
---------------------------------------
`id` int unsigned
`privacy` enum('EVERYONE','FRIENDS')
`quotes_tags`
---------------------------------------
`qt_id` int unsigned
`subject` int unsigned
Subject is a foreign key, `id` on `users`.
I am trying to get the user id (same as subject) of the 5 people who have the most entries in `quotes_tags`. In the same query, I am trying to also get that user's `privacy` entry.
And here is the current query that I am currently using:
Code:
SELECT
quotes_tags.subject,
users.privacy_setting AS privacy,
COUNT(quotes_tags.subject) AS count
FROM `quotes_tags`, `users`
GROUP BY subject
ORDER BY count DESC
LIMIT 0, 5
And that will return a dataset that looks something like:
However, the data is incorrect. 568623702's privacy is FRIENDS, and all of the counts are wrong.
If you can see what I'm trying to do, please help me get my query straightened up. If you are confused about what I'm trying to do, I will try to explain more, or create a simplified case with simple data.
Joined: 02 May 2004 Posts: 5574 Location: toronto, canada
Posted: Thu Feb 07, 2008 5:50 am Post subject:
SELECT
quotes_tags.subject,
users.privacy_setting AS privacy,
COUNT(quotes_tags.subject) AS count
FROM `quotes_tags`, `users`
GROUP BY subject
ORDER BY count DESC
LIMIT 0, 5
I notice that you are referencing privacy_setting as another field, that may be part of the problem... _________________ Lostboy
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum