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

This gives me an error:

# 1248 - Each view must have its own alias

+4
source share
3 answers

You need to specify the aliases of your temporary 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 
+6
source

Do you want to receive the full amount?

 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" ) 
+3
source

The following form should complete the following task:

 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 a subquery.

+1
source

All Articles