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

时间:2013-12-29 05:52:53

标签: mysql sql

我有一张桌子,床身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

我希望查询根据状态值为每个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(由我的case语句确定)的行对应的整行。

使用MySQL 5.5

1 个答案:

答案 0 :(得分:3)

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

索引(term_id,statUs_code)。

通过这个,我们可以自己加入。而是创建这样的视图。

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