It is currently Sat Feb 04, 2012 3:41 pm

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Problem with Count when selecting from multiple tables.
PostPosted: Wed Feb 06, 2008 3:25 am 
Offline

Joined: Wed Feb 06, 2008 3:06 am
Posts: 1
Location: Ohio
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:
Code:
+-----------+----------+-------+
| subject   | privacy  | count |
+-----------+----------+-------+
|         2 | EVERYONE |     8 |
|         1 | EVERYONE |     4 |
| 568623702 | EVERYONE |     4 |
+-----------+----------+-------+


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.

Thanks a lot!
Jonathon[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 07, 2008 6:50 am 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6498
Location: toronto, canada
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

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


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

Search for:
Jump to:  
cron