0

我有一个带有布尔值的表,我想知道一组 id 是否同时具有这两个值。

此 sql 选择我要测试的产品组:

SELECT 
  cp.ItemID,
  cp.IsUnitsOfMeasure
from grouped_products gp
LEFT JOIN products p ON p.product_id = gp.product_id
LEFT JOIN c_products cp ON cp.ItemID = p.product_id
WHERE gp.group_id=123

我想知道该组的 cp.IsUnitsOfMeasure 是否同时包含 1 和 0。

4

2 回答 2

1

试试这个:

SELECT 
    cp.ItemID,
    SUM(cp.IsUnitsOfMeasure) AS sum,
    COUNT(cp.IsUnitsOfMeasure) AS count,
    from grouped_products gp
LEFT JOIN products p ON p.product_id = gp.product_id
LEFT JOIN c_products cp ON cp.ItemID = p.product_id
WHERE gp.group_id=123
  • 如果sum等于,count则所有行都为 1
  • 如果sum等于 0 那么所有的行都为 0
  • 在所有其他情况下:组中有 1 和 0
于 2012-05-11T10:08:27.073 回答
0
SELECT   gp.group_id,
         SUM(cp.IsUnitsOfMeasure=0) AND SUM(cp.IsUnitsOfMeasure=1) AS ContainsBoth,
FROM     grouped_products AS gp
  LEFT JOIN products      AS  p USING (product_id)
  LEFT JOIN c_products    AS cp ON cp.ItemID = p.product_id
GROUP BY gp.group_id

ContainsBoth将是TRUEFALSE表示每个相应的组是否同时包含一个withc_productIsUnitsOfMeasure=0 另一个with =1

于 2012-05-11T10:09:20.557 回答