2016-04-25 38 views
0

我有问题,当我做一个子查询操作数应包含1列(或多个)子查询的MySQL

select r.Request_ID, 
 
(
 
(select e.ReportsTo, (select e1.FirstName from Employees e1 where e1.NIK = e.ReportsTo) as FirstName, 
 
(select e1.NIK from Employees e1 where e1.NIK=e.NIK) as Attention, 
 
(select e1.FirstName from Employees e1 where e1.NIK=e.NIK) as AttentionName 
 
from Employees e where e.ReportsTo ='CS-NIK-2016-0150' and e.NIK='CS-NIK-2016-0160') 
 
) 
 
from Request r where r.Request_ID='CS-REQ-BDG-201604-10099'

操作数应包含1列(S),为什么呢?

回答

0

使用连接而不是子查询。你的子查询也可能返回多于一行,所以它显示错误。 为避免错误使用限制与每个选择不是最佳解决方案,因为在这种情况下,它将只返回第1行。

select r.Request_ID, 
(
(select e.ReportsTo, (select e1.FirstName from Employees e1 where e1.NIK = e.ReportsTo limit 1) as FirstName, 
e.NIK as Attention, FirstName as AttentionName 
from Employees e where e.ReportsTo ='CS-NIK-2016-0150' and e.NIK='CS-NIK-2016-0160') 
) 
from Request r where r.Request_ID='CS-REQ-BDG-201604-10099' 
相关问题