gpt4 book ai didi

所有值都相同的 MySQL GROUP_CONCAT

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:04 26 4
gpt4 key购买 nike

我有以下查询:

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

最佳答案

尝试使用条件聚合的 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

关于所有值都相同的 MySQL GROUP_CONCAT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44022921/

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