MySQL查询顺序三个差异字段

时间:2012-02-10 13:44:33

标签: mysql database sql-order-by

假设我有三张桌子(贴子,墙壁,照片)

发布(id,post_uid,post_content,post_timestamp)

墙(id,wall_from,wall_to,wall_content,wall_timestamp)

照片(id,photo_gid(图库ID参考),photo_caption,photo_filename,photo_timestamp)

我想要做的就是加入这三个表,但顺序是从post_timestamp,wall_timestamp和photo_timestamp到单个查询或单个结果。

我该怎么办?我应该创建新表,还是可以从三个字段订购?

1 个答案:

答案 0 :(得分:1)

按最新的顺序排序

select *
from post
join wall on ...
join photo on ...
where ...
order by greatest(post_timestamp, wall_timestamp, photo_timestamp) desc

或者,最早要先订购:

...
order by least(post_timestamp, wall_timestamp, photo_timestamp)