当前位置: 代码迷 >> 综合 >> SQL报错Every derived table must have its own alias
  详细解决方案

SQL报错Every derived table must have its own alias

热度:10   发布时间:2023-12-13 22:06:10.0

前言:在使用mysql做count计数多表查询或子查询的时候犯了一个低级的错误:
1248 - Every derived table must have its own alias(每个派生表都必须有自己的别名)

报错sql:

SELECTcount(*) 
FROM(SELECTt.id id,t1.id t1_id,t2.id t2_id FROMt_school tLEFT JOIN t_teacher t1 ON t.teacherid = t1.idLEFT JOIN t_user t2 ON t.userid = t2.id WHERE1 = 1 ) 

执行这条语句就会报错:Every derived table must have its own alias。

简单的SQL构成是:select 字段名 from 表名,上面子查询里面查出来的是数据,没有被当成一个表,使用我们只需要在SQL后面加上AS 表名 给他一个表的名字即可

修改过后的SQL:

SELECTcount(*) 
FROM(SELECTt.id id,t1.id t1_id,t2.id t2_id FROMt_school tLEFT JOIN t_teacher t1 ON t.teacherid = t1.idLEFT JOIN t_user t2 ON t.userid = t2.id WHERE1 = 1 ) AS a

加上表名之后即可解决报错了。

  相关解决方案