gpt4 book ai didi

sql - 选择没有行符合条件的非唯一 ID

转载 作者:行者123 更新时间:2023-11-29 14:10:20 25 4
gpt4 key购买 nike

假设我有这张表,我想选择所有 D 都小于 4 的 ID。在这种情况下,它只会选择 ID 1,因为 2 的 D>4,而 3 的 D>4

+----+---+------+
| ID | D | U-ID |
+----+---+------+
| 1 | 1 | a |
+----+---+------+
| 1 | 2 | b |
+----+---+------+
| 2 | 5 | c |
+----+---+------+
| 3 | 5 | d |
+----+---+------+
| 3 | 2 | e |
+----+---+------+
| 3 | 3 | f |
+----+---+------+

我什至不知道从哪里开始对此进行查询,而且我的 sql 还不够好,不知道要用 google 搜索什么,所以如果之前有人问过这个问题,我很抱歉。

最佳答案

我会简单地做:

select id
from table
group by id
having max(d) < 4;

如果您碰巧想要所有原始行,我会使用窗口函数:

select t.*
from (select t.*, max(d) over (partition by id) as maxd
from t
) t
where maxd < 4;

关于sql - 选择没有行符合条件的非唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39420996/

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