Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Every derived table must have its own alias error in MySQL

Tags:

database

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

like image 762
Trim Avatar asked Dec 28 '22 21:12

Trim


2 Answers

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
like image 169
Jage Avatar answered Dec 30 '22 10:12

Jage


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"
  )
like image 40
ypercubeᵀᴹ Avatar answered Dec 30 '22 10:12

ypercubeᵀᴹ