Jump to content

Recommended Posts

I have the following table structures

tablestructure.jpg

 

My Current Query is as follows.

SELECT COUNT(forum_replies.replyid), forum_replies.*, brothers.first_name, brothers.last_name, brothers.user_id, forum_messages.*,  DATE_FORMAT(forum_messages.dateentered,'%W %b %D %l:%i%p') AS messagedate, DATE_FORMAT(forum_replies.dateentered,'%W %b %D %l:%i%p') AS replydate FROM forum_messages
	  INNER JOIN brothers ON forum_messages.brotherid = brothers.user_id
	  LEFT JOIN forum_replies ON forum_messages.messageid = forum_replies.inresponseto
	  GROUP BY forum_messages.messageid

 

Now I am getting 90% of my desired output, but this is going to be a the home page of a very basic forum that I am trying to build and what I want to have happen is I want there to be a column where it either displays the last date of a the most recent reply, or if not available, the post date, so that the most recent items stay on the top of the list, and where most of my problems are coming from, I want  to sort by this column.  Is this at all possible or must I use an IF Else statement?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/86706-must-this-be-done-with-php/
Share on other sites

Many times people like to use 1 query instead of multiples to re-use the data over and over.  So say you have 5 querries on your page, I would think it would be nice to have 1 query and then just use that data for 5 different sections of your code.

 

You may also want to research how UNION works.  This could give you another way to look at things.

Okay I understand.  In this case, I will be using this for only this one purpose.  I read about the union and I am unsure if a Join or a Union will work better based on the last part of my query's needs.  Which will make it easier to have either the post date or the last reply date, a union or a join?  I would really like to make this in sql only and not php, because I would like to keep the code on the page to a minimum. 

After reading about the IFNULL() function, I am not sure how it applies.  All I want to do is list all of the forum titles, with a count of the replies to that post, and then either display the date the topic was posted(if there are no replies) or display the time/date or the latest reply for each forum message.

With a LEFT JOIN, all of the non-matching record columns will be set to null... that is, if there are no replies, in your case.  So you can use IFNULL() to try and pull the latest reply date/time, otherwise, use the post date.  Make sense?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.