melissa820
01-12-2005, 09:02 PM
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 | |
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 | |