我有一个名为 posts
的表,其中有两个名为 timestamp_added
和 timestamp_edited
的字段。
我想按最新添加或编辑的帖子降序排列。
ORDER BY p.timestamp_added DESC, p.timestamp_edited DESC
,没有给我我需要的东西。
我喜欢这样的东西:
ORDER BY IF p.timestamp_edited > 0 THEN p.timestamp_edited DESC ELSE p.timestamp_added DESC
我该怎么做?我尝试用谷歌搜索这个,但找不到我需要的东西。
最佳答案
未设置时timestamp_edited是否为NULL?
在这种情况下你可以这样做:
ORDER BY IFNULL(p.timestamp_edited, p.timestamp_added) DESC
如果未设置时为0:
ORDER BY IF(p.timestamp_edited > 0, p.timestamp_edited, p.timestamp_added) DESC
关于mysql - 关于MySQL ORDER BY的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7428104/