|
|
|
|
![]() ![]() |
Nov 5 2006, 12:08 PM
Post
#1
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 70 Joined: 5-August 06 From: Rome (born only) Member No.: 27,852 |
hi everyone,
I have been trying to get a script to come up with the total number of comments a news item has under it news ------- id author posted title text news_comments ---------- id nid poster posted comment And I am using this query: SELECT n.id as id, n.author as author, n.posted as date, n.title as title, n.text as text, COUNT(c.id) as num_comments FROM news as n, news_comments as c WHERE n.id = c.nid GROUP BY n.id LIMIT 10 Of course that doesnt work because it only shows news entries that have comments, Ive been playing with using 2 queries but I havent found an efficiant way of doing it. 10x This post has been edited by alexviii: Nov 8 2006, 03:17 PM |
|
|
|
Nov 5 2006, 10:01 PM
Post
#2
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 61 Joined: 4-July 05 Member No.: 8,994 |
If you want to see all news items (regardless of whether or not they match to the comments table), then have a look at the LEFT JOIN option in this page:
http://www.wellho.net/mouth/158_MySQL-LEFT...OUTER-JOIN.html |
|
|
|
Nov 6 2006, 04:45 AM
Post
#3
|
|
|
Premium Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 162 Joined: 10-May 06 Member No.: 23,375 |
Ya LEFT JOIN it.
Well the main feature of LEFT JOIN is that if the table that is being joined is empty with regards to a particular row(here your news item) it will add the table with the same fields but with the value NULL that is treated as false of empty in PHP |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 12th October 2008 - 02:30 PM |