[英]Use higher value as variable in MySQL
Let's say I have 2 tables: 假设我有2张桌子:
people
cars
Now, lets say these 2 tables both have a column for a unix_timestamp() called date_created
现在,假设这两个表都有一个名为
date_created
的unix_timestamp() date_created
How would I write a query that will create a variable based on whichever of the 2 values is higher? 我如何编写一个查询,该查询将基于两个值中的较高者创建一个变量? I need this for ordering.
我需要这个来订购。
Example (which obviously doesn't work): 示例(显然不起作用):
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
Hope this makes sense - any help would be appreciated. 希望这有道理-任何帮助将不胜感激。
You could always use GREATEST() to get the higher date. 您可以始终使用GREATEST()获得更高的日期。 Example below:
下面的例子:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.