-2

我有一个看起来像这样的 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 吗?

提前致谢

4

4 回答 4

1

错误在这部分

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

如您所见,您从子查询中返回了多个列。从子查询中获取值时,它应该只返回列。

尝试使用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
于 2013-02-05T15:37:18.497 回答
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

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

于 2013-02-05T15:38:18.280 回答
0

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

于 2013-02-05T15:37:16.717 回答
0

在部分:

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

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

于 2013-02-05T15:37:58.370 回答