PHP forums, MySQL forums, Web Development resources

Home PageHome    PHP ResourcesTopic List    FAQFAQ    SearchSearch    MemberlistMemberlist    UsergroupsUsergroups 
 RegisterRegister
    ProfileProfile    Log in to check your private messagesLog in to check your private messages    Download the RSS Reader RSS Feed Download the RSS Reader RSS for this forum Log inLog in 

PHP Forum :: MySQL Forum :: Java Script Forum



Problem with Count when selecting from multiple tables.

 
Post new topic   Reply to topic    WeberForums.com Forum Index -> MySQL General
View previous topic :: View next topic  
Author Message
JonathonReinhart



Joined: 06 Feb 2008
Posts: 1
Location: Ohio

PostPosted: Wed Feb 06, 2008 2:25 am    Post subject: Problem with Count when selecting from multiple tables. Reply with quote

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]
Back to top
View user's profile Send private message AIM Address
lostboy



Joined: 02 May 2004
Posts: 5574
Location: toronto, canada

PostPosted: Thu Feb 07, 2008 5:50 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
Display posts from previous:   
WeberTrivia Questions WeberTrivia Questions
 Think you are smart? Prove it!. Try your skills with these questions :
 WeberTrivia QuestionsRecursive arrays and multi-dimensional arrays are one and the same. (PHP and MySQL)
 WeberTrivia QuestionsThe \"cache_dir\" tag of the squid configuration has a default of /var/spool/squid. (Linux)

WeberTrivia Questions



Post new topic   Reply to topic    WeberForums.com Forum Index -> MySQL General All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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






Powered by phpBB © 2001, 2005 phpBB Group
PHP Forum :: MySQL Forum :: Java Script Forum