Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Using And?or In A Mysql Query
alexviii
post 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
Go to the top of the page
 
+Quote Post
jhsmurray
post 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
Go to the top of the page
 
+Quote Post
electron
post 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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Mysql Database Setup : Setting Up Mysql Database(6)
  2. Complete Login System(57)
  3. Increment A Mysql Column(7)
  4. Import From Excel File Into Mysql Database(11)
  5. Mysql Database Size(7)
  6. Subquery In Mysql(5)
  7. Quick Question(10)
  8. Check Referrer To Prevent Linking Yours From Other Sites(8)
  9. Is It Possible To Access The Mysql Remotely?(10)
  10. Qupis : Free Hosting With Php, Mysql, Cpanel. (one Line Text Ad At Bottom)(41)
  11. Mysql Won't Update(4)
  12. Tools Needed!(9)
  13. Database With Mysql++(7)
  14. Mysql-essential-5.0.51 Installion Problem(2)
  15. Mysql, Php, Apache Downloads And Setup Was A Nightmare(10)
  1. Html Form!(4)
  2. Mysql(2)
  3. Create Table - Mysql Code - Help(1)
  4. Remote Access Mysql(8)
  5. Please Explain Me Mysql(4)
  6. How Do I Find My Mysql Password? [resolved](5)
  7. It Went Well For A While(5)
  8. Mysql Db Is Down Getting Following Error(2)
  9. Need Advice On Setting Up Mysql Database.(4)
  10. The Only Reason I Choose Ms Sql Server Rather Than Mysql(0)
  11. Mysql Database Not Working(3)
  12. Creating A Php Login System Using Mysql(10)
  13. Mysql Hacks(1)


 



- Lo-Fi Version Time is now: 12th October 2008 - 02:30 PM