我有以下查詢:每個派生的表必須有自己的別名錯誤在MySQL
SELECT SUM(cost)
FROM (
SELECT s.cost
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
)
UNION (
SELECT p.cost
FROM pizza AS p
WHERE TYPE = "Plain"
AND SIZE = "L"
)
這給了我一個錯誤:
#1248 - 每一個派生表必須有自己的別名
我有以下查詢:每個派生的表必須有自己的別名錯誤在MySQL
SELECT SUM(cost)
FROM (
SELECT s.cost
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
)
UNION (
SELECT p.cost
FROM pizza AS p
WHERE TYPE = "Plain"
AND SIZE = "L"
)
這給了我一個錯誤:
#1248 - 每一個派生表必須有自己的別名
你需要你的別名臨時表
SELECT SUM(cost)
FROM
(
(
SELECT s.cost
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
) AS T1
UNION
(
SELECT p.cost
FROM pizza AS p
WHERE TYPE = "Plain"
AND SIZE = "L"
) AS T2
) AS T
你想要整個Sum嗎?
SELECT
(SELECT SUM(s.cost)
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
)
+
(SELECT SUM(p.cost)
FROM pizza AS p
WHERE p.TYPE = "Plain"
AND p.SIZE = "L"
)
下面的表格應該做的工作:
SELECT SUM(cost) FROM (
SELECT cost FROM sandwiches WHERE name = "Cheese Steak"
UNION
SELECT cost FROM pizza WHERE TYPE = "Plain" AND SIZE = "L"
) as temp
MySQL只需要子選擇一個臨時表名。
這仍然沒有工作:( – Trim 2011-04-28 21:19:52
是的,我不認爲你的查詢結構是正確的。看起來像ypercube給出了一個很好的答案 – Jage 2011-04-28 21:52:04
我認爲你需要另一套括號,開始後的第一個和結束於結束。 – 2011-04-28 21:54:40