gpt4 book ai didi

mysql - 选择与最旧条目匹配的所有条目

转载 作者:行者123 更新时间:2023-11-29 04:35:00 25 4
gpt4 key购买 nike

在 MariaDB/MySQL 中,我有一个这样的表:

table (key1, key2, date) with INDEX(key1, key2)

现在我想取最旧的条目:

SELECT * FROM `table` ORDER BY `date` ASC LIMIT 1

最后是第一个查询中与 key1key2 匹配的所有条目:

SELECT * FROM `table` WHERE `key1` = ? AND `key2` = ?

能否以某种方式将其简化为一个查询?

最佳答案

只需使用加入:

select t.*
from `table` t join
(select t.*
from `table` t
order by `date` asc
limit 1
) tt
on t.key1 = tt.key1 and t.key2 = tt.key2;

关于mysql - 选择与最旧条目匹配的所有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45122399/

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