gpt4 book ai didi

php - 仅当 id 不同时才选择列 MySQL

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:37 25 4
gpt4 key购买 nike

我有下表:

  AMAZON_ID  |    DATE    |  STATUS
1 | 01/03/2014 | Pending
1 | 01/03/2014 | Shipped
2 | 01/04/2014 | Pending
3 | 01/05/2014 | Cancelled
4 | 01/06/2014 | Pending

如何从状态等于Pending的表中选择最早的日期,其中id的计数不超过1,它应该像下面这样:

  AMAZON_ID  |    DATE    |  STATUS
2 | 01/04/2014 | Pending

我想不通,这是我目前所拥有的,但它不起作用:

SELECT date
FROM table
WHERE status = 'Pending'
AND COUNT(id) < 2
ORDER BY date ASC
LIMIT 1;

最佳答案

使用子查询对 COUNT 为 1 的 id 进行 GROUP BY。确保您的 id 是 IN 此子查询的结果,status 为 pending。然后 ORDER BY 日期和 LIMIT 到第一个结果。

SELECT date
FROM table
WHERE
status = 'Pending' AND
id IN (
SELECT id
FROM table
GROUP BY id
HAVING COUNT(*) = 1
)
ORDER BY date ASC
LIMIT 1;

关于php - 仅当 id 不同时才选择列 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032281/

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