用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > mysql教程

mysql“ Every derived table must have its own alias”问题

时间:2015-01-23  来源:西部数据  作者:西部数据

在mysql中查询时发现mysql提示Every derived table must have its own alias错误了,意思就是要有一个别名了,这个问题就这么解决了.

我的查询语句:select count(*) from (select * from ……)  total;

错误提示:Every derived table must have its own alias

这句话的意思是说每个派生出来的表都必须有一个自己的别名,一般在多表查询时,会出现此错误.

因为,进行嵌套查询的时候子查询出来的的结果是作为一个派生表来进行上一级的查询的,所以子查询的结果必须要有一个别名.

把MySQL语句改成:select count(*) from (select * from ……) as total;

问题就解决了,虽然只加了一个没有任何作用的别名total,但这个别名是必须的,代码如下:

  1. select name1 name, java, jdbc, hibernate,total 
  2. from (select sc1.name name1, sc1.mark java 
  3.  from student_course2 sc1 
  4.  where sc1.course='java'as a, 
  5.  (select sc2.name name2, sc2.mark jdbc 
  6.  from student_course2 sc2 
  7.  where sc2.course='jdbc'as b, 
  8.  (select sc3.name name3, sc3.mark hibernate 
  9.  from student_course2 sc3 --phpfensi.com 
  10.  where sc3.course='hibernate'as c, 
  11. (select sc4.name name4,sum(sc4.mark) total 
  12. from student_course2 sc4 group by sc4.nameas d 
  13.   where name1=name2 and name2=name3 and name3=name4 order by total ASC

结果正确:

  1. +----------+------+------+-----------+-------+ 
  2. name     | java | jdbc | hibernate | total | 
  3. +----------+------+------+-----------+-------+ 
  4. | wangwu   |   40 |   30 |        20 |    90 | 
  5. | lisi     |   70 |   60 |        50 |   180 | 
  6. | zhangsan |  100 |   90 |        80 |   270 | 
  7. +----------+------+------+-----------+-------+ 
  8. rows in set (0.02 sec)
来顶一下
返回首页
返回首页
推荐资讯
WiFi太不安全:7岁女孩11分钟内入侵公共网络 WiFi太不安全:7岁女孩11分钟内入侵近期刚刚发布研究说WiFi网络能获得人们手机里多少私人信息,
不服跑个分?人工智能也出现“刷分”乱象 不服跑个分?人工智能也出现“刷分2014年,人工智能领域突然爆发,成为了科研和科技创业的热门
相关文章
栏目更新
栏目热门