我想获得一个活动的所有阶段、班级、状态。问题是某些阶段有几行:
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 |
+-------+-------+-----------+