按日期时间 DESC 排序

时间:2021-05-24 09:54:48

标签: php mysql

总结问题:

我无法让 ORDER BY DESC 工作。 我尝试订购的列是 DATETIME,所以我认为不必进行转换。

描述您尝试过的内容:

NULL 值是我想用不同表中的最新日期时间填充的值 表图像。我可以填写一个时间,但该时间不是这个pilotID # 的最近的pilot_death。我无法在日期时间执行 ORDER BY。

注意:由于这对我来说已经足够复杂了,我在此处的 mediafire 链接中的 sql 查询中提供了数据表:https://www.mediafire.com/file/owucp67djcc8djp/pilot_stats.sql/file

fiddle

之前: image1

代码:

$sql = 'UPDATE pe_DeathAncestoryFinal 
SET pe_DeathAncestoryFinal_lastpilotdeathtime = (
SELECT DISTINCT pe_LogEvent_datetime
FROM pe_LogEvent 
WHERE  pe_DeathAncestoryFinal_pilotid = pe_LogEvent_pilotid AND pe_LogEvent_type = 
"pilot_death"
GROUP BY pe_DeathAncestoryFinal_pilotname
ORDER BY pe_LogEvent_datetime DESC )
WHERE pe_DeathAncestoryFinal_lastpilotdeathtime IS NULL
';

之后:我想要这个日期 -> 2021-04-10 19:36:02 image2

1 个答案:

答案 0 :(得分:0)

UPDATE pe_deathancestoryfinal 
JOIN ( SELECT pe_LogEvent_pilotid,
              MAX(pe_LogEvent_datetime) pe_LogEvent_datetime
       FROM pe_logevent 
       WHERE pe_LogEvent_type = "pilot_death"
       GROUP BY pe_LogEvent_pilotid ) x ON pe_DeathAncestoryFinal_pilotid = pe_LogEvent_pilotid
SET pe_DeathAncestoryFinal_lastpilotdeathtime = pe_LogEvent_datetime
WHERE pe_DeathAncestoryFinal_lastpilotdeathtime IS NULL

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f4866412b4d46b6ec120fbd0b70939ce