gpt4 book ai didi

php - 从与其他 critarias 匹配的行中选择特定列中具有最低值的行

转载 作者:行者123 更新时间:2023-11-29 02:39:37 25 4
gpt4 key购买 nike

我已经创建了一个 SQL 表来将事件数据保存到其中。

每个事件都可以多次出现,当我在网站上过滤它们时 - 我希望每个事件的第一次匹配出现。每个事件都保存在不同的行中,其中包含一个列,用于表示每个事件的一般 event_id 和特定的 occ_id。

我需要从匹配的行中获取 - 每个 event_id 中只有一行,并且它必须是 occ_id 值最低的那一行。

gen_id  | event_id | occ_id | month
------------------------------------
1 | 190 | 1 | 4
2 | 190 | 2 | 4
3 | 190 | 3 | 4
4 | 192 | 1 | 4
5 | 192 | 2 | 4
6 | 192 | 3 | 4
7 | 193 | 1 | 5
8 | 193 | 2 | 5

如果我要查找 month = 4 的事件,我需要获取事件 (gen_id):1,4

如果我正在寻找 month = 5 我只需要获取事件 (gen_id): 7

我的 SQL 查询现在获取匹配事件但没有 occ_id 过滤:

(现在看起来像这样)

SELECT
event_id,
event_title,
occ_id
FROM
table_name
WHERE month = 4
GROUP BY event_id
ORDER BY
event_id
DESC

我也尝试过使用 MIN/MAX,但我猜它不是这种情况下的正确处理程序,或者我用错了......

最佳答案

您想过滤。一种方法是在 WHERE 子句中使用相关子查询:

select t.*
from table_name t
where t.occ_id = (select min(t2.occ_id)
from table_name t2
where t2.event_id = t.event_id
);

但是,最低值似乎总是“1”,所以这也可能有效:

select t.*
from table_name t
where t.month = 4 and
t.occ_id = 1;

要添加month,您可以将其添加到外部查询:

select t.*
from table_name t
where t.month = 4 and
t.occ_id = (select min(t2.occ_id)
from table_name t2
where t2.event_id = t.event_id and
t2.month = t.month
);

关于php - 从与其他 critarias 匹配的行中选择特定列中具有最低值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55611348/

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