Welcome 微信登录

首页 / 数据库 / MySQL / MySQL碰到“Every derived table must have its own alias”类似错误说明

Every derived table must have its own alias这句话的意思是说每个派生出来的表都必须有一个自己的别名一般在多表查询时,会出现此错误。因为,进行嵌套查询的时候子查询出来的的结果是作为一个派生表来进行上一级的查询的,所以子查询的结果必须要有一个别名把MySQL语句改成:select count(*) from (select * from ……) as total;问题就解决了,虽然只加了一个没有任何作用的别名total,但这个别名是必须的select name1 name, java, jdbc, hibernate,total
  from (select sc1.name name1, sc1.mark java
 from student_course2 sc1
 where sc1.course="java") as a,
 (select sc2.name name2, sc2.mark jdbc
 from student_course2 sc2
 where sc2.course="jdbc") as b,
 (select sc3.name name3, sc3.mark hibernate
 from student_course2 sc3
 where sc3.course="hibernate") as c,
 (select sc4.name name4,sum(sc4.mark) total
 from student_course2 sc4 group by sc4.name) as d
  where name1=name2 and name2=name3 and name3=name4 order by total ASC;结果正确:+----------+------+------+-----------+-------+
| name   | java | jdbc | hibernate | total |
+----------+------+------+-----------+-------+
| wangwu | 40 | 30 |        20 |    90 |
| lisi   | 70 | 60 |        50 | 180 |
| zhangsan |  100 | 90 |        80 | 270 |
+----------+------+------+-----------+-------+
3 rows in set (0.02 sec)--------------------------------------分割线 --------------------------------------Ubuntu 14.04下安装MySQL http://www.linuxidc.com/Linux/2014-05/102366.htm《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF http://www.linuxidc.com/Linux/2014-03/98821.htmUbuntu 14.04 LTS 安装 LNMP NginxPHP5 (PHP-FPM)MySQL http://www.linuxidc.com/Linux/2014-05/102351.htmUbuntu 14.04下搭建MySQL主从服务器 http://www.linuxidc.com/Linux/2014-05/101599.htmUbuntu 12.04 LTS 构建高可用分布式 MySQL 集群 http://www.linuxidc.com/Linux/2013-11/93019.htmUbuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb http://www.linuxidc.com/Linux/2013-08/89270.htmMySQL-5.5.38通用二进制安装 http://www.linuxidc.com/Linux/2014-07/104509.htm--------------------------------------分割线 --------------------------------------本文永久更新链接地址