查询#1064的SQL错误

时间:2012-04-04 14:31:47

标签: sql

您好我正在尝试对数据库运行查询,但我一直收到此错误,您是否看到我的查询有任何问题?

SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and not in (SELECT *
FROM `data`
WHERE `Category` LIKE 'beauty%')

感谢名单

6 个答案:

答案 0 :(得分:2)

问题出在这个方面:

  

而不是

核心语法是

and not <field name> in (Select <INDIVIDUAL FIELD> from ...)

您无法在选择所有coumns的where子句中执行子查询(*)您需要在子查询中选择一个单独的列,并将其绑定到主查询中的一列。

所以完整的陈述应该是

SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and `Category` not in (SELECT Category
FROM `data`
WHERE `Category` LIKE 'beauty%')

相当于

SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and `Category`  NOT LIKE 'beauty%'

这是一个更具可读性的IMO

有关在WHERE子句的IN语句中使用子查询的更多信息,请参阅http://beginner-sql-tutorial.com/sql-subquery.htm

向下滚动以查找文章中的“相关子查询”。

答案 1 :(得分:1)

SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and `Category` not in 
    (SELECT `Category`
     FROM `data`
     WHERE `Category` LIKE 'beauty%')

答案 2 :(得分:1)

我认为您的查询应如下所示:

SELECT   *
FROM     `data`
WHERE    `Category` LIKE '%beauty%' 
AND      `Category` NOT IN
(
    SELECT   `Category`
    FROM     `data`
    WHERE    `Category` LIKE 'beauty%'
)

无论如何,我会将查询重写为:

SELECT   *
FROM     `data`
WHERE    `Category` LIKE '%beauty%' 
AND      `Category` NOT LIKE 'beauty%'

答案 3 :(得分:0)

SELECT * FROM `data`
WHERE `Category` LIKE '%beauty%' 
and `Category` not in (SELECT *
                      FROM `data` 
                      WHERE `Category` LIKE 'beauty%')

答案 4 :(得分:0)

...和Category

SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and `Category` not in (SELECT *
FROM `data`
WHERE `Category` LIKE 'beauty%')

答案 5 :(得分:0)

为什么你需要子查询?这应该是您的查询

SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and `Category` not LIKE 'beauty%'