前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql出现错误“ Every derived table must have its own alias”

mysql出现错误“ Every derived table must have its own alias”

作者头像
bear_fish
发布2018-09-20 15:36:35
5.6K0
发布2018-09-20 15:36:35
举报
文章被收录于专栏:用户2442861的专栏

http://blog.sina.com.cn/s/blog_5d2eee260100xu8b.html

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)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年03月25日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档