Mysql子查询差异错误

时间:2014-10-31 15:59:03

标签: mysql

嘿伙计们我实际上是mysql的新手..我写了两种类型的代码,

第一种类型

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;

第一个正如我预期的那样正常,但第二个没有..它会像我一样抛出错误

Unknown column 'ID' in 'field list': select ID from (select NAME from CUSTOMERS ) as m group by SALARY

所以问题是第二种方式可以在mysql中完成,而实际上没有任何错误吗?

希望你们能帮助我......呃

1 个答案:

答案 0 :(得分:0)

您的第二个失败是因为您从没有ID的子查询中请求列ID

select ID,
from (select n.NAME from CUSTOMERS as n) as m
group by SALARY;

它只有Name。如果您希望在主查询中使用它们,则需要将IDSALARY添加到子查询中。

select ID,
from (select n.ID, n.SALARY from CUSTOMERS as n) as m
group by SALARY;

同样@Strawyberry指出......这是无稽之谈。您正在按SELECT子句中没有的字段进行分组......我不知道。你也在不需要被贬低的东西上进行暗示。只是做:

select m.ID,
from CUSTOMERS as m
group by m.SALARY;