关于一个 sql 语句的疑问 - V2EX
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 2437 次点击
    这是一个创建于 1953 天前的主题,其中的信息可能已经有所发展或是发生改变。

    表结构

    +-------+---------------+------+-----+---------+-------+ | 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     Solana     1056 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 18:26 PVG 02:26 LAX 11:26 JFK 14:26
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86