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
gettext
V2EX  ›  MySQL

困惑: MySQL 用户变量的代码执行顺序

  •  
  •   gettext · 2019-11-05 14:32:20 +08:00 · 4433 次点击
    这是一个创建于 1606 天前的主题,其中的信息可能已经有所发展或是发生改变。
    select *, 
        CASE 
        	when @fontage = sc.score then @rank
        	when @fontage := sc.score then @rank := @rank + 1 
        END as rank
    	FROM (select @fontage:=null,@rank:=0) init, 
    		 course, teacher, sc
        where 
            (course.CId=sc.CId
            and course.TId=teacher.TId
            and teacher.Tname = '李四'
            )
    	ORDER BY 
    		sc.score DESC
    

    https://zhuanlan.zhihu.com/p/32137597

    第 34 题,成绩有重复的情况下,查询选修 "李四" 老师所授课程的学生中,成绩最高的学生信息及其成绩
    

    老师就改成“李四”好了

    我希望先对 sc.score 降序排序后再执行 CASE END 语句,

    但实际运行怎么 ORDER BY sc.score DESC 会是最后执行的?

    这不对啊。。。

    7 条回复    2019-11-05 18:28:25 +08:00
    oraZhang
        1
    oraZhang  
       2019-11-05 15:45:51 +08:00   ❤️ 1
    为什么不直接 join 来排序?我觉得那是最直观的吧,你这个变量和 select 多表弄的有点复杂了
    oraZhang
        2
    oraZhang  
       2019-11-05 15:47:51 +08:00   ❤️ 1
    @oraZhang 尤其是你要最高的,不是前几名,这样不需要变量来实现排序吧
    oraZhang
        3
    oraZhang  
       2019-11-05 15:49:48 +08:00   ❤️ 1
    再回复一下,你 SELECT 里面的数据都是结果条出来之后的运算,所以不太可能你要去先去 SELECT 后 ORDER,如果有这个需求,那么可以子查询包裹下
    gettext
        4
    gettext  
    OP
       2019-11-05 15:50:24 +08:00
    @oraZhang #1 我自己没有用多表,上面列出来的是知乎网友的解法,这是不对的。但我就是不理解这种多表情况下 ORDER BY sc.score DESC 会是最后执行的?
    gettext
        5
    gettext  
    OP
       2019-11-05 15:52:12 +08:00
    @oraZhang #3

    ```
    select t1.SId,t1.score,
    CASE
    when @fontage = t1.score then @rank
    when @fontage := t1.score then @rank := @rank + 1
    END as rank
    FROM (select @fontage:=null,@rank:=0) init,
    (select sc.SId, sc.Score from course, teacher, sc
    where course.CId=sc.CId
    and course.TId=teacher.TId
    and teacher.Tname = '李四'
    ORDER BY
    sc.score DESC
    ) as t1
    ```

    用子查询包裹后就对了,我就是疑惑这个执行顺序
    oraZhang
        6
    oraZhang  
       2019-11-05 16:27:11 +08:00   ❤️ 1
    @gettext 就是我上一条说的,SELECT 的语句是在查询最末执行的,比如你没办法用 SELECT 中生成的变量做 WHERE 条件一样, 如 select a as b from t where b > 0;
    hooopo
        7
    hooopo  
       2019-11-05 18:28:25 +08:00 via Android
    用 window function 咯
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2815 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:55 · PVG 22:55 · LAX 07:55 · JFK 10:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.