mysql用一条sql统计某个字段不同值的个数

 

表结构:如下

 

实现的sql语句:

SELECT a.name,a.address,a.leader_name,a.leader_phone,COUNT(type) sum,SUM(case when b.type=0 then 1 else 0 end) buyer,SUM(case when b.type=1 then 1 else 0 end) seller FROM village_shop a left JOIN Village_operation2 b ON a.id=b.village_id  WHERE `op_id` = '12' GROUP BY b.village_id

结果:

 

转载于:https://www.cnblogs.com/dongtong/p/5377774.html

你可能感兴趣的:(mysql用一条sql统计某个字段不同值的个数)