操作数应在查询中包含1列

时间:2013-06-14 18:12:52

标签: php mysql sql mysql-error-1241

我有这个复杂的查询,在执行时会出现Operand should contain 1 column(s)错误。我知道这个错误意味着有一些不需要的括号,但我不知道哪些。

这是查询:

SELECT 
 * 
FROM 
 deals 
WHERE 
 country_id = 2 AND 
 (city_id = 4 OR city_id = 0) AND 
 section_id = 2 AND 
 id IN (
  SELECT 
   deal_id 
  FROM 
   deal_repeat 
  WHERE 
   start_date = '2013-06-14') AND 
   business_id IN (
    SELECT 
     *, ( 3959 * acos( cos( radians('51') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-114') ) + sin( radians('51') ) * sin( radians( lat ) ) ) ) AS distance 
    FROM 
     address 
    HAVING 
     distance < '25'
   ) 
  ORDER BY id DESC

2 个答案:

答案 0 :(得分:2)

而不是

  AND business_id IN (SELECT *, ( 3959 * acos( 

这样做

 AND business_id IN (SELECT  ( 3959 * acos( 

Jonathan所述:SELECT *返回所有列,您应该选择一列来选择business_id

答案 1 :(得分:0)

在不知道你的桌面结构的情况下,这有点难以回答。问题是您通过business_id过滤多列列表(select *)。子查询应该只包含一个列(可能是业务ID列表)。相反,假设您的地址表中有一个业务ID字段,那么这可能是您正在寻找的。

SELECT * 
FROM deals 
WHERE country_id = 2 
    AND city_id IN (4,0) 
    AND section_id = 2 
    AND id IN (
        SELECT deal_id 
        FROM deal_repeat 
        WHERE start_date = '2013-06-14') 
   AND business_id IN (
        SELECT business_id 
        FROM address 
        WHERE 3959*acos(cos(radians('51')) * 
            cos(radians(lat)) * 
            cos(radians(lng)-radians('-114'))+sin(radians('51')) * 
            sin(radians(lat)))  < '25') 
ORDER BY id DESC