- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了一个 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/
非常基本的问题:如何找到随机矩阵中的最高值或最低值。我知道有可能说: a = find(A>0.5) 但我正在寻找的更像这样: A = rand(5,5) A = 0.9388 0.9498
我有这张表: BP1 BP2 ----------------------------- 140 80 153 8
我在这个网站上寻找与查找程序中最高/最低数据值有关的答案,但我发现的只是与数组有关的代码。我的代码中没有数组,因此我假设查找最高值或最低值的技术将以不同的方式执行。 如果您查看我的代码,您可以在输出的
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是一名优秀的程序员,十分优秀!