我该如何更正此查询?

时间:2013-02-05 15:35:40

标签: mysql

我的MySQL查询看起来像这样:

SELECT column1_value, column2_value 
  FROM some_table
 WHERE column2_value IN (a,lot,of,comma,separated,values)
   AND column1_value NOT IN (SELECT column1_value, 
                                    column3_value 
                               FROM some_table 
                              WHERE column3_value = some_number)
ORDER BY something DESC
LIMIT 4

但我不断收到错误,出了什么问题?

我不能做IN而不是IN IN吗?

提前致谢

4 个答案:

答案 0 :(得分:1)

错误在这部分

NOT IN (SELECT column1_value, column3_value 
        FROM some_table 
        WHERE column3_value = some_number)

正如您所看到的,您从子查询中返回了多个列。从子查询中获取值时,它只应返回 ONE 列。

尝试使用LEFT JOIN

SELECT  column1_value, column2_value 
  FROM  some_table
        LEFT JOIN
        (
            SELECT  column1_value,  column3_value 
            FROM    some_table   
            WHERE column3_value = some_number
        ) x ON sometable.column1_value = x.column1_value
 WHERE  column2_value IN (a,lot,of,comma,separated,values) AND
        x.colName IS NULL
ORDER   BY something DESC
LIMIT 4

答案 1 :(得分:1)

问题在于这一行

AND column1_value NOT IN (SELECT column1_value, 
                                column3_value 
                           FROM some_table 
                          WHERE column3_value = some_number)

注意内部子查询?

SELECT column1_value, column3_value 
FROM some_table 
WHERE column3_value = some_number

您在数据集中返回两列!它应该只是

SELECT column1_value 
FROM some_table 
WHERE column3_value = some_number

或者您要测试的任何(单个!)列。

答案 2 :(得分:0)

我认为不允许在第二个子查询中选择多个列。

答案 3 :(得分:0)

在部分中:

(SELECT column1_value, column3_value FROM some_table WHERE column3_value = some_number)

您选择了多个值,它是一个价值元。