开发者

Every derived table must have its own alias error in MySQL

I have the following query:

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"
)

That gives me an error of:

#1248 - Every derived table must have its own alias


You need to alias your temp tables

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


Do you want the whole 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"
  )


The following form should do the job:

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 only requires a temporary table name for the subselect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜