SQL错误:操作数应包含1列

时间:2009-11-11 08:33:40

标签: sql

我有这个查询,我收到错误“操作数应该包含1列”,我的查询中有什么错误?

SELECT * FROM contact AS b WHERE b.id IN 
(
    SELECT * 
    FROM contact AS e 
    WHERE e.firstname LIKE ? 
    OR e.lastname LIKE ? 
    OR e.email LIKE ? 
    OR e.phone LIKE ? 
    OR e.company LIKE ? 
    OR e.profession LIKE ? 
    OR e.mobile LIKE ?
)

5 个答案:

答案 0 :(得分:12)

IN运算符需要一个与您所比较的值匹配的值列表:在您的情况下为columnb.id。所以替换这个

WHERE b.id IN (SELECT * 

用这个

WHERE b.id IN (SELECT id 

答案 1 :(得分:7)

第二个选择应为SELECT id而不是SELECT *

答案 2 :(得分:3)

您的WHERE子句使用IN来确定哪些b.id值与您的查询相关。为了在第二个查询中使用IN,必须只返回一列。

答案 3 :(得分:2)

SELECT * FROM 

上述语句中的问题,因为您选择了多个列,

将其更改为

SELECT * FROM contact AS b WHERE b.id IN (SELECT e.ID FROM contact AS e WHERE e.firstname 
LIKE ? OR e.lastname LIKE ? OR e.email LIKE ? OR e.phone LIKE ? OR e.company LIKE ? OR 
e.profession LIKE ? OR e.mobile LIKE ?)

答案 4 :(得分:1)

    SELECT * FROM contact AS b WHERE b.id IN (SELECT e.Id FROM contact AS e WHERE e.firstname 
LIKE ? OR e.lastname LIKE ? OR e.email LIKE ? OR e.phone LIKE ? OR e.company LIKE ? OR 
e.profession LIKE ? OR e.mobile LIKE ?)

它应该是一个包含与SELECT * FROM contact匹配的值的列,而不是b.id

所以,它应该是SELECT e.Id FROM contact