Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9104773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:56:32+00:00 2026-06-17T01:56:32+00:00

I have a query where I’m trying to sort per row either by the

  • 0

I have a query where I’m trying to sort per row either by the latest postdate or the latest commentdate.

A post could have many comments so I need to get the max comment date for each row. If the postdate is greater than the commentdate cdate = postdate else if not cdate = max commentdate.

I am trying to achieve this using:

CASE WHEN max(wc.date_created) > wp.date_created THEN max(wc.date_created) ELSE wp.date_created END AS cdate

ORDER BY cdate DESC

Any help would be greatly appreciated.

Here is the full query (it seems to populate cdate with the commentdate for every row):

SELECT DISTINCT wp.p_id, wp.type, wp.value, wp.media, wp.youtube, wp.post_type, wp.tagedpersons, wp.title AS thetitle, wp.url, wp.description, wp.cur_image, wp.uip, wp.likes, wp.userid, wp.posted_by, wp.post AS postdata, wu . * , UNIX_TIMESTAMP( ) - wp.date_created AS TimeSpent, wp.date_created, wp.course, 
CASE WHEN max(wc.date_created) > wp.date_created THEN max(wc.date_created) ELSE wp.date_created END AS cdate
FROM wallposts wp
INNER JOIN wallusers wu ON wu.mem_id = wp.userid
INNER JOIN wallcomments wc ON wc.post_id = wp.p_id
WHERE (
          wp.userid IN (".$matches.") OR 
          (wp.userid IN (".$courses.") AND wp.course = 1)  OR 
          wp.userid =".$user_id." OR
          wp.tagedpersons LIKE '%".$user_id."%' OR
          EXISTS (SELECT * FROM wallcomments 
          WHERE wp.p_id = wallcomments.post_id AND wallcomments.tagedpersons LIKE '%".$user_id."%')
)
GROUP BY wp.p_id
ORDER BY cdate DESC

Table structure – INNER JOIN wallcomments wc ON wc.post_id = wp.p_id

CREATE TABLE IF NOT EXISTS `wallposts` (
  `p_id` int(11) NOT NULL AUTO_INCREMENT,
  `post` text NOT NULL,
  `type` varchar(55) NOT NULL,
  `value` int(11) NOT NULL,
  `date_created` int(11) NOT NULL,
  `userid` varchar(255) NOT NULL,
  `posted_by` int(11) NOT NULL,
  `likes` int(11) NOT NULL,
  `media` int(11) NOT NULL,
  `uip` varchar(222) NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `url` text NOT NULL,
  `cur_image` text NOT NULL,
  `post_type` tinyint(1) NOT NULL,
  `youtube` text NOT NULL,
  `tagedpersons` varchar(255) NOT NULL,
  `course` int(255) NOT NULL DEFAULT '0',
  `ctimespent` varchar(255) NOT NULL,
  PRIMARY KEY (`p_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=537 ;

CREATE TABLE IF NOT EXISTS `wallcomments` (
  `c_id` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) NOT NULL,
  `comments` text NOT NULL,
  `date_created` int(11) NOT NULL,
  `post_id` int(11) NOT NULL,
  `clikes` int(11) NOT NULL,
  `uip` varchar(222) NOT NULL,
  `tagedpersons` varchar(255) NOT NULL,
  `deleted` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`c_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=349 ;

Sample data:

wp.date_created and wc.date_created (format) – 1356008534

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:56:33+00:00Added an answer on June 17, 2026 at 1:56 am

    Sorted, works great…..

    LEFT JOIN on the comments table was the key and also changed

    CASE WHEN max(wc.date_created) > wp.date_created THEN max(wc.date_created) ELSE wp.date_created END AS cdate
    

    TO

    greatest(max(wp.date_created), COALESCE(max(wc.date_created),0)) AS cdate
    

    Here is the final query:

    SELECT DISTINCT wp.p_id, wp.type, wp.value, wp.media, wp.youtube, wp.post_type, wp.tagedpersons, wp.title AS thetitle, wp.url, wp.description, wp.cur_image, wp.uip, wp.likes, wp.userid, wp.posted_by, wp.post AS postdata, wu . * , UNIX_TIMESTAMP( ) - wp.date_created AS TimeSpent, wp.date_created, COALESCE(max(wc.date_created),0), wp.course, 
    greatest(max(wp.date_created), COALESCE(max(wc.date_created),0)) AS cdate
    FROM wallposts wp
    INNER JOIN wallusers wu ON wu.mem_id = wp.userid
    LEFT JOIN wallcomments wc ON wc.post_id = wp.p_id
    WHERE (
              wp.userid IN (".$matches.") OR 
              (wp.userid IN (".$courses.") AND wp.course = 1)  OR 
              wp.userid =".$user_id." OR
              wp.tagedpersons LIKE '%".$user_id."%' OR
              EXISTS (SELECT * FROM wallcomments 
              WHERE wp.p_id = wallcomments.post_id AND wallcomments.tagedpersons LIKE '%".$user_id."%' AND deleted = 0)
    )
    GROUP BY wp.p_id
    ORDER BY cdate DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mongoosejs Query , which I am trying to execute. I have:
I have query to show the table like this: but I want to PIVOT
I have query with two MtM relations: $em = $this->getEntityManager(); $qb = $em->createQueryBuilder(); $qb
I have query table copyQuery which has Soil Condition has one of the columns,
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I am using in C# MYsql .I have query that works if I run
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have a query that successfully grabs the unique products from my products table
I have a query in Access 2007. It's worked fine for months, but I'm
I have MongoJS query call: db.users.find({username:eleeist},function(err, docs) { console.log(docs); }); The docs variable looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.