-1

我想获得一个活动的所有阶段、班级、状态。问题是某些阶段有几行:

mysql> select stage,class,status from results where event=3009 and person=81451;
+-------+-------+-----------+
| stage | class | status    |
+-------+-------+-----------+
|  3210 | 52800 | OK        |
|  3209 | 52800 | Cancelled |
|  3208 | 52800 | OK        |
|  3209 | 52798 | OK        |
+-------+-------+-----------+
4 rows in set (0,01 sec)

我只想要每个阶段的行。所以我的想法是使用 group by:

mysql> select stage,class,status from results where event=3009 and person=81451 group by stage;
+-------+-------+-----------+
| stage | class | status    |
+-------+-------+-----------+
|  3208 | 52800 | OK        |
|  3209 | 52800 | Cancelled |
|  3210 | 52800 | OK        |
+-------+-------+-----------+
3 rows in set (0,00 sec)

但在多行的情况下,我希望选择 status=OK 行。我怎样才能做到这一点?

我想最终得到的结果是:

+-------+-------+-----------+
| stage | class | status    |
+-------+-------+-----------+
|  3208 | 52800 | OK        |
|  3209 | 52798 | OK        |
|  3210 | 52800 | OK        |
+-------+-------+-----------+
4

1 回答 1

2

因为OK大于CANCELLED您可以使用MAX()的 .

SELECT  a.*
FROM    results a
        INNER JOIN
        (
            SELECT  stage, MAX(status) status
            FROM    result
            WHERE   event = 3009 
                    AND person = 81451
            GROUP   BY stage
        ) b ON  a.stage = b.stage AND
                a.status = b.status
于 2013-10-25T07:08:05.373 回答