V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
18870715400
V2EX  ›  Python

关于一个 sql 语句的疑问

  •  
  •   18870715400 · 2020-06-08 17:04:10 +08:00 · 2028 次点击
    这是一个创建于 1417 天前的主题,其中的信息可能已经有所发展或是发生改变。

    表结构

    +-------+---------------+------+-----+---------+-------+
    | Field | Type          | Null | Key | Default | Extra |
    +-------+---------------+------+-----+---------+-------+
    | SId   | varchar(10)   | YES  |     | NULL    |       |
    | CId   | varchar(10)   | YES  |     | NULL    |       |
    | score | decimal(18,1) | YES  |     | NULL    |       |
    +-------+---------------+------+-----+---------+-------+
    

    按照学生 SId 统计不同学生的总分数并且显示排名

    我的 sql 语句

    select t1.SId, t1.total_score, count(t2.total_score)+1 rank from 
    (select score.SId, sum(score) total_score from score group by SId) t1 
    left join 
    (select score.SId, sum(score) total_score from score group by SId) t2
    on t1.SId = t2.SId and t2.total_score > t1.total_score
    order by rank asc
    

    报错提示:

    In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 't1.SId'; this is incompatible with sql_mode=only_full_group_by

    mysql 版本:5.7.30

    网上说更改相关的配置就可以, 但是除了更改相关 mysql 配置还有什么办法更改 sql 语句来解决这个错误呢

    我的 sql 语句

    select t1.SId, t1.total_score, count(t2.total_score)+1 rank from 
    (select score.SId, sum(score) total_score from score group by SId) t1 
    left join 
    (select score.SId, sum(score) total_score from score group by SId) t2
    on t1.SId = t2.SId and t2.total_score > t1.total_score
    order by rank asc
    

    报错提示:

    In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 't1.SId'; this is incompatible with sql_mode=only_full_group_by

    mysql 版本:5.7.30

    网上说更改相关的配置就可以, 但是除了更改相关 mysql 配置还有什么办法更改 sql 语句来解决这个错误呢

    8 条回复    2020-06-09 01:27:31 +08:00
    jiorix
        1
    jiorix  
       2020-06-08 17:37:02 +08:00
    最外层的 select 没有写 group by ?
    breadenglish
        2
    breadenglish  
       2020-06-08 17:53:02 +08:00
    >> on t1.SId = t2.SId and t2.total_score > t1.total_score
    这个逻辑也有点问题吧,两个子查询都用 SId 取的总分,再用 SId 相等取出来的不就还是自己么。
    应该是
    on t1.SId != t2.SId and t2.total_score > t1.total_score
    dog82
        3
    dog82  
       2020-06-08 17:53:09 +08:00
    最外层用了聚合函数,但是没分组……
    18870715400
        4
    18870715400  
    OP
       2020-06-08 18:03:07 +08:00 via Android
    @breadenglish 受教了 我写错了 谢谢
    18870715400
        5
    18870715400  
    OP
       2020-06-08 18:05:20 +08:00 via Android
    @dog82 使用了聚合函数不一定需要 group by 吧,不然我原先的好多 sql 语句都有问题
    dog82
        6
    dog82  
       2020-06-08 18:07:24 +08:00
    @18870715400 mysql 好像可以不 group by,这样写不符合 sql 规范
    srlp
        7
    srlp  
       2020-06-09 01:26:42 +08:00
    使用聚合函数,除非全部字段都是聚合了,不然还是要加上 group by 最为规范。这保证了满足 sql 规范,日后迁移到其他 sql 引擎也好迁移。

    mysql 只是某些情况下可以接受不规范的 sql 而已,正如你贴出 this is incompatible with sql_mode=only_full_group_by 说的就是这一回事。
    srlp
        8
    srlp  
       2020-06-09 01:27:31 +08:00
    @dog82 #6 同意层主说的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2094 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:15 · PVG 00:15 · LAX 09:15 · JFK 12:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.