[英]Select SQL where value does not exist
I have a two columns product_id
and category_id
.我有两列
product_id
和category_id
。 I want to select all product_id
that don't have the category_id = 90
我想选择所有没有
category_id = 90
product_id
I use this query, but it returns null.我使用此查询,但它返回 null。 What am I doing wrong?
我究竟做错了什么?
SELECT *
FROM `oc_product_to_category`
WHERE NOT EXISTS (
SELECT *
FROM `oc_product_to_category`
WHERE category_id = 90
)
You aren't correlating the inner and outer queries.您没有关联内部和外部查询。 If there's at least one product with
category_id = 90
the inner query will return some rows, and thus the NOT EXISTS
condition will always be false, and the outer query will return no rows.如果至少有一个产品的
category_id = 90
内部查询将返回一些行,因此NOT EXISTS
条件将始终为假,而外部查询将不返回任何行。 You need to add a condition to specify the inner query and the outer query refer to the same product:您需要添加一个条件来指定内部查询和外部查询引用同一个产品:
SELECT *
FROM `oc_product_to_category` a
WHERE NOT EXISTS (
SELECT *
FROM `oc_product_to_category` b
WHERE category_id = 90 AND
a.product_id = b.product_id -- Here!
)
You can query the oc_product
table and check for products that do not have a corresponding entry in the oc_product_to_category
table with category_id
equal to 90.您可以查询
oc_product
表,并检查没有在相应的条目产品oc_product_to_category
表category_id
等于90。
SELECT *
FROM `oc_product`
WHERE product_id NOT IN
(SELECT product_id
FROM `oc_product_to_category`
WHERE category_id = 90)
Try This尝试这个
SELECT *
FROM `oc_product_to_category`
WHERE category_id <> **90**
try this:尝试这个:
SELECT *
FROM `oc_product_to_category`
WHERE category_id <> 90
SELECT *
FROM `oc_product_to_category` where product_id NOT IN (SELECT product_id
FROM `oc_product_to_category where category_id = 90)
your query should be like this你的查询应该是这样的
SELECT *
FROM `oc_product_to_category` m
WHERE NOT EXISTS (
SELECT *
FROM `oc_product_to_category`
WHERE m.category_id = 90
)
you can get many ways those result like您可以通过多种方式获得这些结果,例如
SELECT *
FROM `oc_product_to_category`
WHERE category_id <> 90
SELECT *
FROM `oc_product_to_category`
WHERE category_id != 90
SELECT *
FROM `oc_product_to_category`
having category_id <> 90
SELECT *
FROM `oc_product_to_category`
WHERE category_id NOT IN
(SELECT *
FROM `oc_product_to_category`
WHERE category_id = 90)
SELECT * FROM oc_product_to_category
WHERE category_id
<> 90 ;
Try this尝试这个
SELECT *
FROM your_Name
WHERE category_id NOT LIKE '90'
Try:尝试:
SELECT *
FROM oc_product_to_category
WHERE category_id not in ('90')
Try this尝试这个
SELECT * FROM oc_product_to_category where category_id != 90
(OR) (或者)
SELECT * FROM oc_product_to_category where category_id <> 90
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.