MySQL GROUP_CONCAT具有相同的所有值

时间:2017-05-17 10:57:38

标签: mysql

我有以下查询:

SELECT * FROM charges WHERE (
  charges.id not in (
    select charge_id from billing_invoice_charges where is_deactivated = 0
  )
)

我需要将其转换为JOIN QUERY,所以我正在尝试:

SELECT charges.id, group_concat(bic.is_deactivated) AS active_statuses 
FROM charges LEFT JOIN billing_invoice_charges AS bic
ON bic.charge_id = charges.id GROUP BY charges.id

HAVING .......; <--- Check if all values are 1's

GROUP_CONCAT的输出是:

+------+-----------------+
| id   | active_statuses |
+------+-----------------+
|    2 | 0,1,1           |
|    3 | 1,1             |
|    6 | 1               |
|    7 | 1,1,1           |
|   12 | 0,0,1           |
+------------------------+

如何查看HAVING子句中active_statuses的所有值是否为1?这应该给我charges我正在寻找。

2 个答案:

答案 0 :(得分:3)

尝试使用条件聚合的HAVING子句,以确保每个组都不会出现非1个状态。

SELECT
    charges.id,
    GROUP_CONCAT(bic.is_deactivated) AS active_statuses 
FROM charges
LEFT JOIN billing_invoice_charges AS bic
    ON bic.charge_id = charges.id
GROUP BY charges.id
HAVING SUM(CASE WHEN bic.is_deactivated <> 1 THEN 1 ELSE 0 END) = 0

答案 1 :(得分:1)

尝试:

    private void RangeBase_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        var slider = sender as Slider;


        if (slider == null) return;


        var progressBar = (ProgressBar)slider.Template.FindName("TrackProgressBar", slider);


        if (progressBar == null) return;


        if (e.NewValue > progressBar.Value)
        {
            slider.Value = progressBar.Value;
            e.Handled = true;
            return;
        }
    }

应该忽略active_statuses中带0的任何结果