Hey guys i am new to mysql actually ..I have wrote two types of code,
select ID,
(select n.NAME from CUSTOMERS as n group by SALARY ) as m
from CUSTOMERS;
select ID,
from (select n.NAME from CUSTOMERS as n) as m
group by SALARY;
The first one works fine as i expected but the second one didnt ..It throws me error like
Unknown column 'ID' in 'field list': select ID from (select NAME from CUSTOMERS ) as m group by SALARY
So by question is can the second way be done in mysql without any errors actually ??
Hope you guys can help me out..Thx
Your second one fails because you request column ID
from a subquery that does not have ID
:
select ID,
from (select n.NAME from CUSTOMERS as n) as m
group by SALARY;
It only has Name
. You will need to add both ID
and SALARY
to your subquery if you wish to use them in your main query.
select ID,
from (select n.ID, n.SALARY from CUSTOMERS as n) as m
group by SALARY;
Also as @Strawyberry pointed out... it's nonsense. You are grouping by a field that isn't in your SELECT clause which... I don't know. You are also subquerying on something that doesn't need to be subqueried. Just do:
select m.ID,
from CUSTOMERS as m
group by m.SALARY;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments