gpt4 book ai didi

mysql - req_type 在一个查询中明智地计算 id

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

我有一个 sql 查询问题。我不想为相同的结果执行三次查询。

在我的表中,我有一个字段 req_type,它有三个参数,要么 1,要么 2,要么 3。

我希望在一个查询中基于 req_type 的计数器,而不是像下面这样执行 3 次查询

select count(id) as premium FROM tablename where req_type=1
select count(id) as premium1 FROm tablename where req_type=2
select count(id) as premium2 FROm tablename where req_type=3

我卡住了,有人能帮帮我吗?

最佳答案

您可以使用 case 进行此类计数

select sum(case when req_type=1 then 1 else 0 end) as premium,
sum(case when req_type=2 then 1 else 0 end) as premium1,
sum(case when req_type=3 then 1 else 0 end) as premium2
FROM tablename

关于mysql - req_type 在一个查询中明智地计算 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200907/

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