close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues


sort sql by date from 2 different tables

Thread began 2/22/2010 11:00 am by sam308940 | Last modified 2/23/2010 9:52 pm by Eric Mittman | 821 views | 1 replies |


sort sql by date from 2 different tables


i have 2 tables.
1 table is a my MAIN MESSAGE POST
the other table is the COMMENTS posted to messages.

my recordset pulls the main message, and then pulls the JOINED comments matching that
what i need to do is the get date field from both tables and have the data sorted by the most recent date.

everything i have tried i either get things sorted by the most recent message where messages with comments do not get identified as newly updated - they are just sorted by the date of the original message.

OR I get the most recent comment posting date, where the new messages posted with no comments go to the end.

What i need is to get the records sorted so that they sort by the most recent.
Recent meaning most recent message posted, or recent comment.

heres the code i have currently for this recordset.
any help would be much appreciated.

Dim rsMainPage
Dim rsMainPage_cmd
Dim rsMainPage_numRows

Set rsMainPage_cmd = Server.CreateObject ("ADODB.Command")
rsMainPage_cmd.ActiveConnection = MM_connBoard_STRING
rsMainPage_cmd.CommandText = "SELECT blg_topic_top.id_top, blg_topic_top.title_top, blg_article_art.id_art, blg_article_art.title_art, blg_article_art.description_art, blg_article_art.date_art , blg_article_art.visible_art, count(blg_comment_com.id_com) AS count_id_com_1, max(blg_comment_com.date_com), blg_article_art.text_art FROM ((blg_topic_top LEFT JOIN blg_article_art ON blg_article_art.idtop_art=blg_topic_top.id_top) LEFT JOIN blg_comment_com ON blg_comment_com.idart_com=blg_article_art.id_art) WHERE blg_article_art.visible_art=1 GROUP BY blg_topic_top.id_top, blg_topic_top.title_top, blg_article_art.id_art, blg_article_art.title_art, blg_article_art.description_art, blg_article_art.date_art, blg_article_art.visible_art, blg_article_art.text_art ORDER BY max(blg_comment_com.date_com) DESC"
rsMainPage_cmd.Prepared = true

Set rsMainPage = rsMainPage_cmd.Execute
rsMainPage_numRows = 0


Sign in to reply to this post

Eric Mittman

This is a complex scenario to deal with directly in the sql. It might be possible but I'm not sure how you would structure it.

I think it might be best to have a third table to help you organize all of the messages. When a message or comment is inserted you would insert a new record into this table to record the time, the message id and comment id. In the end this would give you a table that has all of the posts organized by the order they were added. Populating this table with the existing records would put you in about the same scenario you are in now though.

This is not an easy problem to solve. The alternative would be to use some php code to hold the data from both tables then sort the data and use that as your order of the records.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question





Ease of use


security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...