[英]Why do these queries return different results?
SELECT DISTINCT productName, listPrice
FROM product
WHERE productID IN (SELECT productID FROM saleItem WHERE quantity = 2)
AND productID NOT IN (SELECT productID FROM saleItem WHERE quantity <> 2)
Hey everyone, I'm querying a database for a class to recap the lessons from the semester and am a bit confused. 嘿大家,我正在查询一个数据库,以便从本学期回顾教训,我有点困惑。 The query above returns a different number of results than the query below. 上面的查询返回的结果数不同于下面的查询。 It's an entry level course and I still can't figure out how quantity = 2
differs from NOT in (SELECT productID FROM saleItem WHERE quantity <> 2)
. 这是一个入门级课程,我仍然无法弄清楚quantity = 2
与NOT in (SELECT productID FROM saleItem WHERE quantity <> 2)
区别NOT in (SELECT productID FROM saleItem WHERE quantity <> 2)
。 The way I am reading the syntax seems they should only return a quantity of 2. If anyone could help, It would be hugely appreciated! 我正在阅读语法的方式似乎他们应该只返回2的数量。如果有人可以提供帮助,那将非常感激! If this a poorly formatted question I apologize. 如果这是一个格式不正确的问题,我道歉。
SELECT DISTINCT productName, listPrice
FROM product
WHERE productID IN (SELECT productID FROM saleItem WHERE quantity = 2)
Consider product IDs that appear in multiple rows of saleItem
: 考虑出现在saleItem
多行中的产品ID:
quantity
of 2
某些行的quantity
为2
quantity
different from 2
其他一些行的quantity
与2
不同 If you have two saleItem
rows for the same productId
, and at least one of these rows has quantity not equal to 2
, the row corresponding to that productId
would be excluded from the first query. 如果同一productId
有两个saleItem
行,并且这些行中至少有一行的数量不等于2
,则对应于该productId
的行将从第一个查询中排除。
For example, if your saleItem
looks like this 例如,如果您的saleItem
看起来像这样
saleItemId ProductId Quantity
---------- --------- --------
100 1 2
101 2 3
102 2 2
then the first query would exclude productId=2
, while the second query would include it. 那么第一个查询将排除productId=2
,而第二个查询将包括它。
I still can't figure out how quantity = 2 differs from NOT in (SELECT productID FROM saleItem WHERE quantity <>2) 我仍然无法弄清楚数量= 2与NOT的区别(SELECT productID FROM saleItem WHERE quantity <> 2)
Probably NULL values, you could simple check it with: 可能是NULL值,您可以使用以下方法进行简单检查:
SELECT distinct productName, listPrice
FROM product
WHERE productID NOT IN (SELECT COALESCE(productID,-1)
FROM saleItem
WHERE quantity <> 2) --assuming that quantity is NOT NULL
Let's assume your tables with following sample data: 让我们假设您的表包含以下示例数据:
Products: 产品介绍:
productID | productName | listprice
-------------------------------------
1 | A | 100
-------------------------------------
2 | B | 200
-------------------------------------
3 | C | 300
-------------------------------------
4 | D | 400
saleItem: saleItem:
productId | quantity
----------------------
1 | 2
----------------------
2 | 2
----------------------
3 | 5
----------------------
4 | Null
----------------------
1 | 3
Note: I have assumed here that quantity
field doesn't have Not Null
constraint. 注意:我假设quantity
字段没有Not Null
约束。
Query : 查询:
SELECT productID FROM saleItem WHERE quantity =2
Result: 结果:
productID
-----------
1
-----------
2
-----------
1
Query : 查询:
SELECT distinct productID FROM saleItem WHERE quantity =2
Result: 结果:
productID
-----------
1
-----------
2
Query: 查询:
SELECT productID FROM saleItem WHERE quantity <> 2 // <> operator won't include rows with quantity having NULL values
Result: 结果:
productID
------------
1
-----------
3
-----------
1
Query: 查询:
SELECT distinct productID FROM saleItem WHERE quantity <> 2
Result: 结果:
productID
------------
1
-----------
3
Hence, Query 1 can be converted in: 因此,查询1可以转换为:
SELECT distinct productName, listPrice
FROM product
WHERE productID IN (1,2,1)
AND productID NOT in (1,3,1);
Results: 结果:
productID | productName | listprice
-------------------------------------
2 | B | 200
So, Query 1 is showing results of productID
which only has one value for quantity
and that is 2
. 因此,查询1显示的productID
结果只有一个quantity
值,即2
。
Query 2: 查询2:
SELECT distinct productName, listPrice
FROM product
WHERE productID IN (1,2,1);
Results: 结果:
productID | productName | listprice
-------------------------------------
1 | A | 100
-------------------------------------
2 | B | 200
So, Query2 will return info for those productID
who has 2
as quantity but may or may not have other values quantity
as well. 因此,Query2将返回那些具有2
作为数量但也可能具有或不具有其他值quantity
productID
信息。
Last Note: 最后注意:
1) quantity <> 2
won't include rows with NULL quantity. 1) quantity <> 2
将不包括具有NULL数量的行。 But Not IN ( quantity <> 2)
will include rows that has value for quantity
other than 2
including NULL
. 但Not IN ( quantity <> 2)
将包含具有除2
之外的quantity
值的行,包括NULL
。
Hope it helps: 希望能帮助到你:
I cannot find the difference between the two queries you provided. 我找不到您提供的两个查询之间的区别。 However, based on your description, I am guessing this is your second query: 但是,根据您的描述,我猜这是您的第二个查询:
SELECT distinct productName, listPrice FROM product WHERE productID NOT IN ( SELECT productID FROM saleItem WHERE quantity <>2)
Maybe I am missing out on something because the query looks like it should return the same results to me. 也许我错过了一些东西,因为查询看起来应该向我返回相同的结果。 I can only think of what explanation is that NULL has no value so it cannot be compared. 我只能想到NULL没有值的解释,因此无法进行比较。 It is possible that there are some records that have NULL as the value for quantity. 有些记录可能有NULL作为数量值。 Then this query: SELECT productID FROM saleItem WHERE quantity <>2
will not return those records. 然后这个查询: SELECT productID FROM saleItem WHERE quantity <>2
将不返回那些记录。
So in the outer query, it selects NOT IN
so since the record that has NULL quantity is not in inner query, you will have it in your final result. 因此在外部查询中,它选择NOT IN
,因为具有NULL数量的记录不在内部查询中,您将在最终结果中使用它。
In short and in conclusion, the second query will have records with quantity = 2 and quantity = NULL. 简而言之,第二个查询将包含quantity = 2和quantity = NULL的记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.