您好我正在尝试在数据库上运行查询,但我不断收到此错误,您是否发现我的查询有任何问题?
SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and not in (SELECT *
FROM `data`
WHERE `Category` LIKE 'beauty%')
谢谢
您好我正在尝试在数据库上运行查询,但我不断收到此错误,您是否发现我的查询有任何问题?
SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and not in (SELECT *
FROM `data`
WHERE `Category` LIKE 'beauty%')
谢谢
问题出在这方面:
而不是在
正确的语法是
and not <field name> in (Select <INDIVIDUAL FIELD> from ...)
您不能在选择所有列的 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
向下滚动以在文章中找到“相关子查询”。
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 IN
(
SELECT `Category`
FROM `data`
WHERE `Category` LIKE 'beauty%'
)
无论如何,我会将查询重写为:
SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%'
AND `Category` NOT LIKE 'beauty%'
SELECT * FROM `data`
WHERE `Category` LIKE '%beauty%'
and `Category` not in (SELECT *
FROM `data`
WHERE `Category` LIKE 'beauty%')
...和Category
?
SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and `Category` not in (SELECT *
FROM `data`
WHERE `Category` LIKE 'beauty%')
为什么你需要一个子查询?这应该是您的查询
SELECT *
FROM `data`
WHERE `Category` LIKE '%beauty%' and `Category` not LIKE 'beauty%'