从同一类别中选择多个产品?

j8ag8udp  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(405)

语句尝试1:
它似乎忽视了一切,从任何地方获取产品,而不分国家

SELECT *
FROM products
WHERE `country` = 'America' AND
      product = 'Product 1' AND
      product = 'Product 2' AND
      completed = 'complete'

语句2:
这样做的结果是返回0个结果,即使结果是假设存在的。

SELECT *
FROM products
WHERE `country` = 'America' AND
      product = 'Product 1' OR
      product = 'Product 2' AND
      completed = 'complete'
kdfy810k

kdfy810k1#

您的第一个查询在逻辑上不正确,原因是 product = product 1 AND product = product 2 ,where子句逐个筛选行,因此一行不能同时包含product列中的两个值。
您的第二个查询有一个您介绍的问题 OR 如果产品2在另一个国家/地区的状态为“已完成”,则忽略国家/地区筛选器。要更正您的查询,您需要对您的产品进行分组 OR 使用括号的条件 (condition 1 or condition 2) and some other condition ```
SELECT *
FROM products
WHERE country = 'America'
AND (product = 'Product 1' OR product = 'Product 2')
AND completed = 'complete'

或者你可以用 `IN` 条款来简化你的标准

SELECT *
FROM products
WHERE country = 'America'
AND product IN('Product 1','Product 2')
AND completed = 'complete'

相关问题