If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Problem with query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-27-07, 13:43
Eiolon Eiolon is offline
Registered User
 
Join Date: Jan 2006
Posts: 26
Problem with query

I made a forum. All works well but with one exception. If a reply has not been made to a topic, the topic does not appear on the list of topics.

Here is the query I am using:

Code:
SELECT t.*, r.*, DATE_FORMAT(t.topic_last_reply, '%c/%e/%Y %l:%i %p') AS convdate FROM topics t JOIN replies r ON (t.topic_id = r.reply_topic) WHERE t.topic_forum = ".$_GET['forum_id']." GROUP BY t.topic_subject ORDER BY t.topic_last_reply DESC
When a reply is made, it increments the value in topic_num_replies +1. So what I did is add a HAVING clause:

Code:
SELECT t.*, r.*, DATE_FORMAT(t.topic_last_reply, '%c/%e/%Y %l:%i %p') AS convdate FROM topics t JOIN replies r ON (t.topic_id = r.reply_topic) WHERE t.topic_forum = ".$_GET['forum_id']." GROUP BY t.topic_subject HAVING t.topic_num_replies >= 0 ORDER BY t.topic_last_reply DESC
I get the same result. As soon as I make a reply to the topic it appears on the list.

Thanks for your advice!
Reply With Quote
  #2 (permalink)  
Old 11-27-07, 13:59
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
You want to change your JOIN from an INNER to a LEFT on the replies table.
The reason the second query didn;t erturn aresult is because when there are no replies then the count is NULL, not zero
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 11-27-07, 15:00
Eiolon Eiolon is offline
Registered User
 
Join Date: Jan 2006
Posts: 26
Thanks for your reply.

I made it a LEFT join but the results are still the same. I am running this in MySQL directly to make sure no PHP is getting in the way.

Thanks for your help.
Reply With Quote
  #4 (permalink)  
Old 11-27-07, 16:47
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
post the SQL when using a left join please
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 11-27-07, 17:15
Eiolon Eiolon is offline
Registered User
 
Join Date: Jan 2006
Posts: 26
Hey man, thanks but I got it working. I don't know what I did. Maybe it was just getting a new browser open.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On