我在MySQL上運行此查詢 spa
SELECT ID FROM ( SELECT ID, msisdn FROM ( SELECT * FROM TT2 ) );
它給出了這個錯誤: code
每一個派生表必須具備本身的別名。 it
是什麼致使此錯誤? table
這是一個不一樣的例子,沒有別名就不能重寫(不能GROUP BY DISTINCT
)。 軟件
想象一下一個稱爲purchases
的表,該表記錄了customers
在stores
進行的購買,即,這是一個多對多表,而且軟件須要知道哪些顧客在多個商店中進行了購買: 引用
SELECT DISTINCT customer_id, SUM(1) FROM ( SELECT DISTINCT customer_id, store_id FROM purchases) GROUP BY customer_id HAVING 1 < SUM(1);
..將因錯誤而中斷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);
(注意AS custom
別名)。 tab
我認爲這是在要求您這樣作: co
SELECT ID FROM (SELECT ID, msisdn FROM (SELECT * FROM TT2) as myalias ) as anotheralias;
可是,爲何首先要編寫此查詢? block
每一個派生表(AKA子查詢)確實必須有一個別名。 也就是說,括號中的每一個查詢都必須被賦予一個別名( AS whatever
),能夠在外部查詢的其他部分中使用它來引用它。
SELECT ID FROM ( SELECT ID, msisdn FROM ( SELECT * FROM TT2 ) AS T ) AS T
固然,對於您而言,整個查詢能夠替換爲:
SELECT ID FROM TT2