在MySQL中使用更高的值作为变量

时间:2015-05-13 15:58:30

标签: mysql sql select

我们说我有两张桌子:

people
cars

现在,假设这两个表都有一个名为date_created的unix_timestamp()列

我如何编写一个查询,根据2个值中的较高者创建变量?我需要这个来订购。

示例(显然不起作用):

select p.name, c.model, higher_value(p.date_created, c.date_created) as higher_date
from `people` as p
left join `cars` as c on (c.id_person = p.id)
order by higher_date desc

希望这是有道理的 - 任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

你总是可以使用GREATEST()来获得更高的日期。示例如下:

select p.name, c.model, GREATEST(p.date_created, c.date_created) as higher_date
from `people` as p
left join `cars` as c on (c.id_person = p.id)
order by GREATEST(p.date_created, c.date_created) desc