MySQL 中的“每个派生表都必须有自己的别名”是什么错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1888779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the error "Every derived table must have its own alias" in MySQL?
提问by silverkid
I am running this query on MySQL
我在 MySQL 上运行这个查询
SELECT ID FROM (
SELECT ID, msisdn
FROM (
SELECT * FROM TT2
)
);
and it is giving this error:
它给出了这个错误:
Every derived table must have its own alias.
每个派生表都必须有自己的别名。
What's causing this error?
是什么导致了这个错误?
回答by Paul
Every derived table (AKA sub-query) must indeed have an alias. I.e. each query in brackets must be given an alias (AS whatever
), which can the be used to refer to it in the rest of the outer query.
每个派生表(AKA 子查询)确实必须有一个别名。即括号中的每个查询都必须有一个别名 ( AS whatever
),它可以用于在外部查询的其余部分中引用它。
SELECT ID FROM (
SELECT ID, msisdn FROM (
SELECT * FROM TT2
) AS T
) AS T
In your case, of course, the entire query could be replaced with:
当然,在您的情况下,整个查询可以替换为:
SELECT ID FROM TT2
回答by hometoast
I think it's asking you to do this:
我认为它要求你这样做:
SELECT ID
FROM (SELECT ID,
msisdn
FROM (SELECT * FROM TT2) as myalias
) as anotheralias;
But why would you write this query in the first place?
但是,您为什么首先要编写此查询?
回答by Neil Stockbridge
Here's a different example that can't be rewritten without aliases ( can't GROUP BY DISTINCT
).
这是一个不同的例子,没有别名就不能重写( can't GROUP BY DISTINCT
)。
Imagine a table called purchases
that records purchases made by customers
at stores
, i.e. it's a many to many table and the software needs to know which customers have made purchases at more than one store:
想象一个名为at的表purchases
,它记录了customers
at 的购买情况stores
,即它是一个多对多的表,软件需要知道哪些客户在多个商店进行了购买:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases)
GROUP BY customer_id HAVING 1 < SUM(1);
..will break with the error Every derived table must have its own alias
. To fix:
..会因错误而中断Every derived table must have its own alias
。修理:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases) AS custom
GROUP BY customer_id HAVING 1 < SUM(1);
( Note the AS custom
alias).
(注意AS custom
别名)。