Skip to content Skip to sidebar Skip to footer

How To Select The Max Of Two Element Of Each Row In Mysql

I have got a table that is a result of a (My)SQL query. In this table I have the post creation timestamp and the user comment creation timestamp. The trick is that not all posts ha

Solution 1:

Given your table looks something like this...

CREATETABLE `yourtable` (
  `id` bigint(20) unsigned NOTNULL AUTO_INCREMENT,
  `post_creation` timestampNULLDEFAULTNULL,
  `comment_creation` timestampNULLDEFAULTNULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

...the SELECT-query could be done like this:

SELECTIF(comment_creation > post_creation, 
          comment_creation, 
          post_creation) AS sortorder,
       id
FROM yourtable
ORDERBY sortorder DESC;

Post a Comment for "How To Select The Max Of Two Element Of Each Row In Mysql"