Home > Back-end >  Can´t figure out the problem with MYSQL query
Can´t figure out the problem with MYSQL query

Time:10-21

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?

CodePudding user response:

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

CodePudding user response:

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
  • Related