在使用MYSQL进行嵌套查询时出现:Every derived table must have its own alias错误解决方法
Posted 浪子尘晨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在使用MYSQL进行嵌套查询时出现:Every derived table must have its own alias错误解决方法相关的知识,希望对你有一定的参考价值。
在做多表查询,或者查询的时候产生新的表的时候会出现这个错误:Every derived table must have its own alias(每一个派生出来的表都必须有一个自己的别名)。
delete from stock
where (org_id,material_id,state)
in (SELECT * from
(select org_id,material_id, state from stock WHERE state = 1 group by org_id,material_id,state having count(*) > 1)
)
当执行这条sql语句的时候就会出现Every derived table must have its own alias
(select org_id,material_id, state from stock WHERE state = 1 group by org_id,material_id,state having count(*) > 1)
就会产生一张新的表,和前面的表stock联合查询,但是mysql要求每一个派生出来的表都必须有一个自己的别名,那我给派生表加上别名即可;
eg:修改后的sql,直接在新生产的表中加入 他的别命名就行(“as a”或者“a”),“a”为新表的别名,这样就解决问题了
delete from stock
where (org_id,material_id,state) in
(SELECT * from (select org_id,material_id, state
from stock WHERE state = 1 group by org_id,material_id,state having count(*) > 1) as a )
以上是关于在使用MYSQL进行嵌套查询时出现:Every derived table must have its own alias错误解决方法的主要内容,如果未能解决你的问题,请参考以下文章
Redshift Spectrum 在创建嵌套数据时出现语法错误
如何使用 Laravel 5.6 在 Laravel Eloquent 中实现嵌套 MySQL 查询