gpt4 book ai didi

sql - 查询返回多个相同的行而不是一个

转载 作者:行者123 更新时间:2023-11-29 14:18:49 25 4
gpt4 key购买 nike

我有表格:juicesjuice_ingredientsingredients

juices 表具有以下属性:

  • 姓名
  • 条形码

juice_ingredients 表具有以下属性:

  • juice_id
  • ingredient_id

ingredients 表有

  • 姓名
  • 可选( bool 值)

为了客户的物流,各种果汁可能有相同的条形码,但成分不同,其中一些是可选的我需要通过条形码选择其成分中不含可选成分的单一果汁。

我注册了四种成分:水(可选:false)、糖(可选:true)、菠萝果肉(可选:false)和薄荷(可选:true)。并签了四种果汁:一种只有水和菠萝果肉,一种是水,菠萝果肉和糖,一种是水,菠萝果肉和薄荷,还有一种是水,菠萝果肉,薄荷和糖。都带有相同的条形码。我查询仅选择使用非可选成分制成的果汁,在本例中为水和菠萝果肉。

SELECT *
FROM juices
INNER JOIN juice_ingredients ON (juice_ingredients.juice_id = juices.id)
INNER JOIN ingredients ON (juice_ingredients.ingredient_id = ingredients.id)
WHERE juices.barcode = '000000000001' AND ingredients.optional = false

但是它返回了多行。应该如何更改此查询以仅携带一种或成分中不包含可选成分的果汁?

最佳答案

您可以使用 having 子句:

SELECT juices.*
FROM juices
JOIN juice_ingredients ON juice_ingredients.juice_id = juices.id
JOIN ingredients ON juice_ingredients.ingredient_id = ingredients.id
WHERE juices.barcode = '000000000001'
GROUP BY 1, 2
HAVING MAX(ingredients.optional::text) = 'false'

关于sql - 查询返回多个相同的行而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37495390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com