gpt4 book ai didi

mysql - SQL根据计算列的最小值获取整行

转载 作者:可可西里 更新时间:2023-11-01 06:48:51 24 4
gpt4 key购买 nike

我有一个表格,其中包含多个状态不同的床位 ID 值,类似这样。

PID     |  term_id   |  student_id   |  bed_id   | status    |  Comment
--------+------------+---------------+-----------+-----------+----------------
1 | 29 | 1234 | 751 | Canceled | Not this one
2 | 29 | 1234 | 751 | Active | This one
3 | 29 | 531 | 752 | Active | This one too
4 | 29 | 823 | 752 | Canceled | Not this one either
5 | 29 | 525 | 753 | Canceled | But this one too

我想要一个查询,根据 status 的值为每个 bed_id 获取一行。
我试过:

SELECT *,MIN(CASE sample.status
WHEN 'Arrived' THEN 1
WHEN 'Active' THEN 2
WHEN 'Pending Approval' THEN 3
WHEN 'Pending Confirmation' THEN 4
WHEN 'Pending Manual' THEN 5
WHEN 'Denied' THEN 6
WHEN 'Canceled' THEN 7
END) AS StatusOrder
FROM sample
WHERE (sample.term_id = 29)
GROUP BY bed_id

但它给了我:

PID  |  term_id |  student_id |  bed_id | status    |  Comment           | StatusOrder
------------------------------------------------------------------------------------
1 | 29 | 1234 | 751 | Canceled | Not this one | 2
3 | 29 | 531 | 752 | Active | This one too | 2
5 | 29 | 525 | 753 | Canceled | But this one too | 7

(StatusOrder值是对的,但该行的其余部分与StatusOrder值最小的行不对应)

我想要的是:

PID  |  term_id |  student_id |  bed_id | status    |  Comment           | StatusOrder
------------------------------------------------------------------------------------
2 | 29 | 1234 | 751 | Active | This one | 2
3 | 29 | 531 | 752 | Active | This one too | 2
5 | 29 | 525 | 753 | Canceled | But this one too | 7

我读过 MySQL MIN/MAX all row也试过这个:

SELECT *,MIN(CASE sample.status
WHEN 'Arrived' THEN 1
WHEN 'Active' THEN 2
WHEN 'Pending Approval' THEN 3
WHEN 'Pending Confirmation' THEN 4
WHEN 'Pending Manual' THEN 5
WHEN 'Denied' THEN 6
WHEN 'Canceled' THEN 7
END) AS StatusOrder
FROM sample
WHERE (
(sample.term_id = 29) AND (
StatusOrder = CASE sample.status
WHEN 'Arrived' THEN 1
WHEN 'Active' THEN 2
WHEN 'Pending Approval' THEN 3
WHEN 'Pending Confirmation' THEN 4
WHEN 'Pending Manual' THEN 5
WHEN 'Denied' THEN 6
WHEN 'Canceled' THEN 7
END)
)
GROUP BY bed_id

但这会产生错误。 (还尝试用完整的 CASE 语句替换 StatusOrder)

注意:我已经简化了包含更多列的实际表格。但基本上我需要访问与每个 bed_id 具有最低 StatusOrder 的行(由我的案例语句确定)相对应的整行。

使用 MySQL 5.5

最佳答案

下面的查询有效,但最好考虑用代码替换 status_code 列状态,并有一个单独的表 status(status_code,description) {denormalise

和索引(term_id,statUs_code)。

这样,我们就可以自己加入了。而是创建这样的 View 。

SELECT * FROM
(SELECT *,(CASE sample.status
WHEN 'Arrived' THEN 1
WHEN 'Active' THEN 2
WHEN 'Pending Approval' THEN 3
WHEN 'Pending Confirmation' THEN 4
WHEN 'Pending Manual' THEN 5
WHEN 'Denied' THEN 6
WHEN 'Canceled' THEN 7
END) AS status_code FROM SAMPLE
WHERE sample.term_id = 29
) my_view1,
(SELECT BED_ID,MIN(CASE sample.status
WHEN 'Arrived' THEN 1
WHEN 'Active' THEN 2
WHEN 'Pending Approval' THEN 3
WHEN 'Pending Confirmation' THEN 4
WHEN 'Pending Manual' THEN 5
WHEN 'Denied' THEN 6
WHEN 'Canceled' THEN 7
END) AS status_code FROM SAMPLE
WHERE sample.term_id = 29
GROUP BY BED_ID
) my_view2
WHERE my_view1.bed_id = my_view2.bed_id
AND my_view1.status_code = my_view2.status_code

关于mysql - SQL根据计算列的最小值获取整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20822574/

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