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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x