Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-12-2005, 09:02 PM   PM User | #1
melissa820
New Coder

 
Join Date: Oct 2004
Posts: 64
Thanks: 2
Thanked 0 Times in 0 Posts
melissa820 is an unknown quantity at this point
query problem using count and group by

I can't seem to get this query to work.

What I am trying to do is make a list of all the journal records in the 'faculty_journals' table, and then for each journal name, show a count of how many records in the 'faculty_articles' table contain that journal id. (A record in faculty_articles has a corresponding journal_id that is the id number from faculty_journals). For example if there are 2 records in faculty_articles that list journal id of 5, and in faculty_journals #5 is "Journal of SQL", then I want to display "Journal of SQL: 2 articles". Make sense?

The problem with the query below is that it won't list journals that have no articles associated with them. Here is the query I have tried which does not work:

SELECT a.id, a.journal_name, a.weight, COUNT(b.journal_id) as num FROM faculty_journals a LEFT JOIN faculty_articles b ON a.id=b.journal_id GROUP BY b.journal_id ORDER BY a.journal_name;

The tables are as follows:
faculty_journals:

| id | int(11) | | PRI | NULL | auto_increment |
| journal_name | varchar(255) | YES | | NULL | |
| weight | double(4,2) | YES | | NULL | |


faculty_articles

| id | int(11) | | PRI | NULL | auto_increment |
| authors | varchar(255) | YES | | NULL | |
| title | varchar(255) | YES | | NULL | |
| journal_id | int(5) | YES | | NULL | |

Last edited by melissa820; 01-12-2005 at 09:13 PM..
melissa820 is offline   Reply With Quote
Old 01-12-2005, 09:22 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
hmm ...

mysql doesn't handle GROUP BY like ANSI SQL. For instance, with standard SQL, a.id, a.journal_name, a.weight should be part of the group by or an agregation-function.
Maybe your results are caused by mysql's deviating implemantation...

try
SELECT a.id, a.journal_name, a.weight, COUNT(*) as num FROM faculty_journals a LEFT JOIN faculty_articles b ON a.id=b.journal_id GROUP BY a.id, a.journal_name, a.weight ORDER BY a.journal_name;
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-12-2005, 09:29 PM   PM User | #3
melissa820
New Coder

 
Join Date: Oct 2004
Posts: 64
Thanks: 2
Thanked 0 Times in 0 Posts
melissa820 is an unknown quantity at this point
I tried out the query you suggested, this one lists all the journals but the 'num' counts are incorrect... instead of displaying a count of articles for that journal, it just displays '1' every time. Here's an example:

| id | journal | weight | num |
+---+------------------------------------------------+--------+-----+
| 1 | 60 Labour Arbitration Cases | 0.00 | 1 |
| 2 | 98 Labour Arbitration Cases | 0.00 | 1 |
| 3 | Academy of Accounting and Finance | 0.00 | 1 |
| 4 | Academy of Marketing Science | 0.00 | 1 |
| 5 | Accounting and Business Review | 0.00 | 1 |


I want the 'num' field to reflect how many journal articles have that id in the journal_articles table.

As far as I knew, I had to group by faculty_articles.journal_id, because these are the totals that I wanted to get? Hmm...

Last edited by melissa820; 01-12-2005 at 09:37 PM..
melissa820 is offline   Reply With Quote
Old 01-12-2005, 09:43 PM   PM User | #4
melissa820
New Coder

 
Join Date: Oct 2004
Posts: 64
Thanks: 2
Thanked 0 Times in 0 Posts
melissa820 is an unknown quantity at this point
Thanks to you, I've got something that works:

select a.id, a.journal_name, a.weight, count(b.journal_id) as num from faculty_journals a left join faculty_articles b on a.id=b.journal_id group by a.id, a.journal_name, a.weight order by a.journal_name;

Thank you so much! I would have never thought of that other grouping.
melissa820 is offline   Reply With Quote
Old 01-12-2005, 10:24 PM   PM User | #5
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
glad you got it working.

still strange though... the RDBM apparently will process the OUTER JOIN differently based on the way that you agregate the recordset. it looks like that if you only include fields from one of the tables inside the GROUP BY and agregationfunctions, that the grouping is then performed before the join. where as if you include fields from both tables in the GROUP BY and agregationfunction, then it looks as if the join is performed first and then the grouping is done on the resulting recordset (as would be the expected behaviour).
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:16 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.