Tomáš Strejček
Tomáš Strejček

Reputation: 56

Can´t figure out the problem with MYSQL query

Hey Guys I have this query

SELECT SUM(body), SUM(mice), SUM(sety) FROM
(
  SELECT sum(body1) as body, sum(mice1) as mice, sum(sety1) as sety from utkani where hrac1 in(select hrac1 from skupina where id=66) 
  UNION ALL
  SELECT sum(body2) as body, sum(mice2) as mice, sum(sety2) as sety from utkani where hrac2 in(select hrac1 from skupina where id=66) 
)

and I am getting error:

#1248 - Every derived table must have its own alias

Can you help me with this please?

Upvotes: 0

Views: 45

Answers (2)

Ankit Jindal
Ankit Jindal

Reputation: 4050

I think you missed adding an alias to the virtual table you are querying

SELECT SUM(body), SUM(mice), SUM(sety) FROM
(
  SELECT sum(body1) as body, sum(mice1) as mice, sum(sety1) as sety from utkani where hrac1 in(select hrac1 from skupina where id=66) 
  UNION ALL
  SELECT sum(body2) as body, sum(mice2) as mice, sum(sety2) as sety from utkani where hrac2 in(select hrac1 from skupina where id=66) 
) as temp

Upvotes: 2

nbk
nbk

Reputation: 49410

A deivd table is that what youi have afuter the first FROM clause

As it is a tbale, it must a name like utkani but as you see a t1 is enough

SELECT SUM(body), SUM(mice), SUM(sety) FROM
(
  SELECT sum(body1) as body, sum(mice1) as mice, sum(sety1) as sety from utkani where hrac1 in(select hrac1 from skupina where id=66) 
  UNION ALL
  SELECT sum(body2) as body, sum(mice2) as mice, sum(sety2) as sety from utkani where hrac2 in(select hrac1 from skupina where id=66) 
) t1

Upvotes: 1

Related Questions