gpt4 book ai didi

php - 通过将两个 MySQL 查询合并为 1 来简化它们?

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

我有两个非常相似的 MySQL 查询,但是它们在特定的 WHERE 子句(type)上返回一个 COUNT(),所以我想知道它们是否可以合并到 1 个查询中,因为只有 WHERE 子句 (type) 区分了两者?

查询 1:

SELECT COUNT(referral_id) AS in_count
FROM referrals
WHERE author = '{$author}'
AND type = 'in'
AND ip_address = '{$ip_address}'
LIMIT 1

查询 2:

SELECT COUNT(referral_id) AS out_count
FROM referrals
WHERE author = '{$author}'
AND type = 'out'
AND ip_address = '{$ip_address}'
LIMIT 1

非常感谢所有帮助:B

最佳答案

SELECT SUM(IF(type='in',1,0)) as in_count, 
SUM(IF(type='out',1,0)) as out_count
FROM referrals
WHERE author = '{$author}' AND
ip_address = '{$ip_address}'
LIMIT 1

关于php - 通过将两个 MySQL 查询合并为 1 来简化它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4637772/

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