暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL在where子句中引用取别名的列

原创 只是甲 2020-08-11
1042

备注:测试数据库版本为MySQL 8.0

如需要scott用户下建表及录入数据语句,可参考:
scott建表及录入数据sql脚本

我们写sql的过程中,经常会遇到别名的问题,也经常因为别名的问题报错
下面是一个where应用别名列的问题

-- where直接用别名列,报错 select sal as salary,co from emp where salary < 5000; -- 用内联视图可以解决这个问题 select * from ( select sal as salary,comm as commission from emp ) x where salary < 5000;

执行结果:

mysql> select sal as salary,comm as commission -> from emp -> where salary < 5000; ERROR 1054 (42S22): Unknown column 'salary' in 'where clause' mysql> mysql> mysql> mysql> select * -> from ( -> select sal as salary,comm as commission -> from emp -> ) x -> where salary < 5000; +---------+------------+ | salary | commission | +---------+------------+ | 1600.00 | 1300.00 | | 1250.00 | 1500.00 | | 2975.00 | 500.00 | | 1250.00 | 2400.00 | | 2850.00 | 500.00 | | 2450.00 | 500.00 | | 3000.00 | 500.00 | | 1500.00 | 0.00 | | 1100.00 | 500.00 | | 950.00 | 500.00 | | 3000.00 | 500.00 | | 1300.00 | 500.00 | +---------+------------+ 12 rows in set (0.01 sec)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论