为什么该第三查询不返回第一查询和第二查询的总行数?

时间:2019-01-22 23:25:27

标签: sql-server

如果下面的第一个查询返回1377行,第二个查询返回765行, 为什么第三个查询返回1806行而不是预期的1377 + 765 = 2142行?

SELECT DISTINCT item_id FROM MyTable 
WHERE(COMMENTS LIKE '%Created by Upload #90323%')
ORDER BY item_id

SELECT DISTINCT item_id FROM MyTable
WHERE (COMMENTS LIKE '%Created by Upload #90324%')
ORDER BY item_id

SELECT DISTINCT item_id FROM MyTable
where (COMMENTS LIKE '%Created by Upload #90323%') 
   or (COMMENTS LIKE '%Created by Upload #90324%')
ORDER BY item_id

1 个答案:

答案 0 :(得分:4)

由于DISTINCT,两个item_id值将具有相同的COMMENT值。为了证明这一点,请从所有查询中删除DISTINCT,然后将看到行数匹配。