V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
xuyl
V2EX  ›  MySQL

简单的 sql 查询问题,求教

  •  
  •   xuyl · 2015-06-13 14:45:01 +08:00 · 3580 次点击
    这是一个创建于 3241 天前的主题,其中的信息可能已经有所发展或是发生改变。

    表如下:

    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也查出来?

    21 条回复    2015-06-13 22:30:29 +08:00
    Cee
        1
    Cee  
       2015-06-13 15:05:03 +08:00
    SELECT name, sex, max(score) AS score FROM score GROUP BY sex;
    xuyl
        2
    xuyl  
    OP
       2015-06-13 15:10:04 +08:00
    @Cee group by是对sex聚合分组,这样取出来的name是不对应score的。
    caigaopei
        3
    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
        4
    xuyl  
    OP
       2015-06-13 15:17:17 +08:00
    @caigaopei 你看一下结果,tony是错的,应该是andy
    Gonster
        5
    Gonster  
       2015-06-13 15:19:05 +08:00
    >.<要不按分数排个序
    staticor
        6
    staticor  
       2015-06-13 15:19:08 +08:00
    groupby max取得 socre + id+ sex, 再join 添加上name.
    Cee
        7
    Cee  
       2015-06-13 15:22:52 +08:00
    @xuyl 嗯,那還是做個子查詢或者 Join 好了
    caigaopei
        8
    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
        9
    caigaopei  
       2015-06-13 15:52:01 +08:00
    @xuyl 好像还是不对.. 菜鸟飘过...
    feiyuanqiu
        10
    feiyuanqiu  
       2015-06-13 15:57:51 +08:00
    跟这个问题类似,可以直接参考这里 8L 的回答:

    https://www.v2ex.com/t/193875
    liboyue
        11
    liboyue  
       2015-06-13 16:01:57 +08:00 via Android
    这样行不?
    select name, max(score) from score where sex = man
    zqhong
        12
    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
        13
    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
        14
    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
        15
    kid813  
       2015-06-13 17:52:26 +08:00 via iPhone
    在SELECT语句中,所有不出现在聚合函数(MAX)的列名都必须出现在GROUP BY中。

    但是写GROUP BY name显然不是想要的结果,所以应该只能写子查询。
    bigfella
        16
    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
        17
    bin456789  
       2015-06-13 18:01:12 +08:00
    @bigfella 如果第二高分的男生跟第一高分的女生同分,那结果就有误了
    lilydjwg
        18
    lilydjwg  
       2015-06-13 18:43:02 +08:00
    @zqhong 我也是这么想的~
    ETiV
        19
    ETiV  
       2015-06-13 18:50:15 +08:00 via iPhone
    这标题起的太误导人了……

    MySQL 就没有能解决这类问题的“简单的SQL语句”
    omengye
        20
    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
    bigfella
        21
    bigfella  
       2015-06-13 22:30:29 +08:00
    @bin456789 摁~ 没想到, 改了下, 加了性别的条件,应该差不错吧
    select * from (select t.sex, max(t.score) as score from score t group by sex) aa, score bb where bb.sex = aa.sex and bb.score = aa.score
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   972 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 76ms · UTC 22:51 · PVG 06:51 · LAX 15:51 · JFK 18:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.