嵌套查询出错

时间:2012-08-01 10:04:20

标签: mysql

这是我的疑问:

SELECT id, DeviceName, DeviceTypeId
FROM device
WHERE DeviceTypeId LIKE (SELECT DeviceId FROM net_backupdevice WHERE CustomerId = '1');
执行错误后显示

  

错误1242(21000):子查询返回超过1行

此表net_backupdevice返回2条记录,我希望记录信息id,DeviceName,DeviceTypeId存储在设备表中。

所以请帮忙。

2 个答案:

答案 0 :(得分:0)

SELECT id, DeviceName, DeviceTypeId FROM device WHERE DeviceTypeId IN (select DeviceId from net_backupdevice where CustomerId = '1');

请注意IN而不是Like

答案 1 :(得分:0)

最简单的修复

SELECT id, DeviceName, DeviceTypeId 
FROM device 
WHERE DeviceTypeId IN (select DeviceId from net_backupdevice where CustomerId = '1');

可能这是一个正确的版本

SELECT id, DeviceName, DeviceTypeId 
FROM device d inner join net_backupdevice nb on d.DeviceTypeId = nb.DeviceId
WHERE nb.CustomerId = 1;