gpt4 book ai didi

mysql - 过滤 IN 语句

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

我需要优化这个sql选择:

SELECT ch_num, payment_type, Total FROM transactions WHERE ( ch_num ) 
IN (
SELECT ch_num FROM transactions GROUP BY ch_num HAVING count('ch_num') > 1
)
ORDER BY `ch_num`

我得到的结果是:

ch_num  payment_type    Total 
20001 visa 36.60
20001 visa 36.60
20001 mc 30.60
50019 cash 9.00
50019 mc 18.95
50023 cash 2.70
50023 visa 7.00

但我只需要存在“现金” payment_type 的结果行,因此应省略“ch_no”20001。正确的结果是:

ch_num  payment_type    Total 
50019 cash 9.00
50019 mc 18.95
50023 cash 2.70
50023 visa 7.00

最佳答案

SELECT ch_num, payment_type, Total
FROM transactions
WHERE ch_num IN
(
SELECT ch_num
FROM transactions
GROUP BY ch_no
HAVING count('ch_num') > 1
and sum(payment_type='cash') >= 1
)
ORDER BY `ch_num`

关于mysql - 过滤 IN 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925385/

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