gpt4 book ai didi

mysql - 使用 WHERE 子句选择 MAX 日期

转载 作者:行者123 更新时间:2023-11-29 13:15:38 26 4
gpt4 key购买 nike

我有一个名为 technical 的表,我可以在哪里放置搜索函数的 WHERE 子句?

我想插入WHERE t1.blklot LIKE '01/01' - 此行将仅显示那些blklot = 01/01

SELECT * 
FROM technical t1
JOIN (
SELECT blklot, MAX(date_update) AS MAXDATE
FROM technical
GROUP BY blklot
) t2 ON t1.blklot = t2.blklot
AND t1.date_update = t2.MAXDATE
ORDER BY t1.blklot

最佳答案

您实际上可以将其放置在子查询或外部查询中。在子查询中,您需要更改条件中的表别名。您的条件是用于聚合的 key ,因此它可以在任一位置工作。

或者,您可以将其放在 on 子句中:

ON t1.blklot = t2.blklot and
t1.date_update = t2.MAXDATE and
t1.blklot LIKE '01/01'

编辑:

其实就是放在子查询中。这是最有效的方法:

SELECT * 
FROM technical t1 JOIN
(SELECT blklot, MAX(date_update) AS MAXDATE
FROM technical
WHERE blklot LIKE '01/01'
) t2
ON t1.blklot = t2.blklot AND
t1.date_update = t2.MAXDATE
ORDER BY t1.blklot;

由于您仅选择一个值,因此不需要group by

关于mysql - 使用 WHERE 子句选择 MAX 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21500010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com