简单的 sql 查询问题,求教

2015-06-13 14:45:01 +08:00
 xuyl

表如下:

score

id | name | sex | score|

1 | tony | man | 80 |

2 | lily | woman | 90 |


3 | lucy | woman | 85 |


4 | andy | man | 95 |


要查出man和woman的最高分,如下


sex | name | score |


man | andy | 95 |


woman | lily | 90 |


select 语句怎么写?
我是菜鸟,只会写这样 select sex,max(score) as score from score group by score; 能否不用子查询就把name也查出来?

3599 次点击
所在节点    MySQL
21 条回复
Cee
2015-06-13 15:05:03 +08:00
SELECT name, sex, max(score) AS score FROM score GROUP BY sex;
xuyl
2015-06-13 15:10:04 +08:00
@Cee group by是对sex聚合分组,这样取出来的name是不对应score的。
caigaopei
2015-06-13 15:15:01 +08:00
select name, sex ,max(score) from score group by sex;
+------+-------+------------+
| name | sex | score |
+------+-------+------------+
| tony | man | 95 |
| lily | woman| 90 |
+------+-------+------------+
xuyl
2015-06-13 15:17:17 +08:00
@caigaopei 你看一下结果,tony是错的,应该是andy
Gonster
2015-06-13 15:19:05 +08:00
>.<要不按分数排个序
staticor
2015-06-13 15:19:08 +08:00
groupby max取得 socre + id+ sex, 再join 添加上name.
Cee
2015-06-13 15:22:52 +08:00
@xuyl 嗯,那還是做個子查詢或者 Join 好了
caigaopei
2015-06-13 15:43:05 +08:00
@xuyl select name,sex,max(score) from score group by name LIMIT 0,2; 这个笨办法不知道可行不..

+------+-------+------------+
| name | sex | max(score) |
+------+-------+------------+
| andy | man | 95 |
| lily | woman | 90 |
+------+-------+------------+
caigaopei
2015-06-13 15:52:01 +08:00
@xuyl 好像还是不对.. 菜鸟飘过...
feiyuanqiu
2015-06-13 15:57:51 +08:00
跟这个问题类似,可以直接参考这里 8L 的回答:

https://www.v2ex.com/t/193875
liboyue
2015-06-13 16:01:57 +08:00
这样行不?
select name, max(score) from score where sex = man
zqhong
2015-06-13 16:52:12 +08:00
我的笨方法:
SELECT name, sex, max(score) AS score FROM score WHERE sex = man
UNION
SELECT name, sex, max(score) AS score FROM score WHERE sex = womon;
bin456789
2015-06-13 17:13:50 +08:00
我也有个笨方法
在mssql上可用

SELECT *
FROM ( SELECT TOP 1
Sex ,
Name ,
Score
FROM dbo.Score
WHERE Sex = 'man'
ORDER BY Score DESC
) AS t1
UNION ALL
SELECT *
FROM ( SELECT TOP 1
Sex ,
Name ,
Score
FROM dbo.Score
WHERE Sex = 'woman'
ORDER BY Score DESC
) AS t2;

Sex Name Score
-------------------------------------------------- -------------------------------------------------- -----------
man andy 95
woman lily 90
bin456789
2015-06-13 17:33:27 +08:00
找到一个更好的查询方法,请自己翻译到mysql中
SELECT *
FROM ( SELECT * ,
RANK() OVER ( PARTITION BY Sex ORDER BY Score DESC ) AS rank
FROM dbo.Score
) AS t
WHERE t.rank = 1;
kid813
2015-06-13 17:52:26 +08:00
在SELECT语句中,所有不出现在聚合函数(MAX)的列名都必须出现在GROUP BY中。

但是写GROUP BY name显然不是想要的结果,所以应该只能写子查询。
bigfella
2015-06-13 17:57:03 +08:00
子查询可行~ 笨方法
select name, sex, score from Score where score in (select max(score) from Score group by sex )
bin456789
2015-06-13 18:01:12 +08:00
@bigfella 如果第二高分的男生跟第一高分的女生同分,那结果就有误了
lilydjwg
2015-06-13 18:43:02 +08:00
@zqhong 我也是这么想的~
ETiV
2015-06-13 18:50:15 +08:00
这标题起的太误导人了……

MySQL 就没有能解决这类问题的“简单的SQL语句”
omengye
2015-06-13 19:32:37 +08:00
还是来比一比办法笨好了
SELECT
s1.sex,
s2.NAME,
s1.score
FROM
(
SELECT
sex,
max(score) AS score
FROM
score
GROUP BY
sex
) s1
LEFT JOIN score s2 ON s1.score = s2.score
ORDER BY s1.score DESC

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/198241

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX