where型子查詢

想查出成績最高的那行數據,但是不能使用order by和limit,使用where子查詢來做.

#表中的所有數據.
mysql> select * from sc;
+-----+-----+-------+
| SNO | CNO | SCORE |
+-----+-----+-------+
| 1   | K1  |    83 |
| 2   | K1  |    85 |
| 2   | K5  |    90 |
| 5   | K1  |    92 |
| 5   | K5  |    84 |
| 5   | K8  |    80 |
+-----+-----+-------+
6 rows in set (0.00 sec)

#成績最高的分數.
mysql> select max(score) from sc;
+------------+
| max(score) |
+------------+
|         92 |
+------------+
1 row in set (0.01 sec)

#兩個where子句結合起來查詢.
mysql> select sno,cno,score from sc where score=(select max(score) from sc);
+-----+-----+-------+
| sno | cno | score |
+-----+-----+-------+
| 5   | K1  |    92 |
+-----+-----+-------+
1 row in set (0.01 sec)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章