Every derived table must have its own alias

末蓝、 2022-12-30 14:50 208阅读 0赞

A derived table is an expression that generates a table within the
scope of a query FROM clause. For example, a subquery in a SELECT
statement FROM clause is a derived table:

  1. SELECT ... FROM (subquery) [AS] tbl_name ...

The [AS] tbl_name clause is mandatory because every table in a FROM clause must have a name. Any columns in the derived table must have unique names.

mysql from内部subquery,也称作派生查询,必须要设置tbl_name的。
因为FROM子句中的每个表都必须具有名称。

  1. //错误 Every derived table must have its own alias
  2. select * from (select * from user where age >=2 )
  3. //正确
  4. select * from (select * from user where age >=2 ) as u
  5. select * from (select * from user where age >=2 ) u

还需要注意的是:派生表中的任何列都必须具有唯一的名称。

  1. //错误 Duplicate column name 'id'
  2. select * from (select user.id as id , user.name as id from user where age >=2 ) as a
  3. //正确
  4. select * from (select user.id as id , user.name as name from user where age >=2 ) as a

详细参考:mysql derived tables

发表评论

表情:
评论列表 (有 0 条评论,208人围观)

还没有评论,来说两句吧...

相关阅读