mysql - 如何在重复语句中显示 0?

标签 mysql sql

我试图显示所有政策业务部门,但仅在存在值时显示

我的表:

|policies|
  |id|  |client|  |policy_business_unit_id|  |cia_ensure_id|   |state|       
   1      MATT                  1                   1             0
   2      STEVE                 1                   1             0
   3      BILL                  1                   2             0
   4      LARRY                 1                   2             1
   5      MATT                  1                   2             1
   6      STEVE                 2                   2             2
   7      BILL                  2                   2             2
   8      LARRY                 2                   2             1
   9      MATT                  2                   1             1

|policy_business_units|
   |id|   |name|  |comercial_area_id|
     1     LIFE         1 
     2     ROB          1 
     3     CAR          1 
     4     RARE         1 
     5     RARE2        1 

|comercial_areas|
   |id|   |name|
    1      MICROSOFT
    2      APPLE

|cia_ensures|
   |id|   |name|
    1      ORANGE
    2      BT
    3      ATT
    4      MOVISTAR
    5      SPRINT

我得到的结果没问题,但没有显示所有 policy_business 单位

 UNIT  V1  A1  N1   V2 A2  N2   V3  A3  N3   V4  A4  N4   V5  A5  N5
 LIFE  2   0   0    1   2  0    0   0   0     0   0   0   0    0   0
 ROB   0   1   0    0   1  2    0   0   0     0   0   0   0    0   0 

这里是查询 http://sqlfiddle.com/#!2/6909d8/1

set @@local.group_concat_max_len=10000;
select distinct @sql := concat('SELECT pb.name as unit,',
group_concat(concat(
'SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,', 
 SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,', 
 SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),
' 
FROM cia_ensures ce 
LEFT JOIN policies p on ce.id = p.cia_ensure_id
INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id
INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id
AND ca.id=1  
Group by p.policy_business_unit_id;')
from cia_ensures
where id in(1,2,3,4,5,6,7,8,10);

prepare stmt from @sql;
execute stmt;

我试图证明这一点:

 UNIT  V1  A1  N1   V2 A2  N2   V3  A3  N3   V4  A4  N4   V5  A5  N5
 LIFE  2   0   0    1   2  0    0   0   0     0   0   0   0    0   0
 ROB   0   1   0    0   1  2    0   0   0     0   0   0   0    0   0 
 RARE  0   0   0    0   0  0    0   0   0     0   0   0   0    0   0 
 RARE2 0   0   0    0   0  0    0   0   0     0   0   0   0    0   0 

有人可以帮助我吗?

我将感谢所有的帮助。

最佳答案

如果对您有帮助,请检查以下更改,我没有执行它只是在查询中做了一些更改,所以需要在您最后检查:

set @@local.group_concat_max_len=10000;
select distinct @sql := concat('SELECT pb.name as unit,',
group_concat(concat(
'SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,', 
 SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,', 
 SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),
' 
FROM 
policy_business_units pb 
LEFT JOIN policies p 
ON pb.id = p.policy_business_unit_id
LEFT JOIN cia_ensures ce 
on ce.id = p.cia_ensure_id 
LEFT JOIN comercial_areas ca 
ON ca.id = pb.comercial_area_id
AND ca.id=1  
Group by pb.name;')
from cia_ensures
where id in(1,2,3,4,5,6,7,8,10);

prepare stmt from @sql;
execute stmt;

关于mysql - 如何在重复语句中显示 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23226972/

相关文章:

php - Wordpress多用户、自定义数据库、使用全局wpdb时潜在的并发问题

php - 我如何查询wordnet数据库(mysql)以获取一个词的所有同义词和相似词?

mysql - 查询中的列总和

mysql - 以下哪种方法导入数据库更快?

mysql - 不同表之间的条件连接以组合不同的列

php - 如何通过第三方 API 安全维护用户身份验证?

SQL 连接问题...后续步骤

mysql - 使用 MYSQL 跨表选择数据的连接语句出现问题

sql - 在 delete 语句中忽略聚簇索引和覆盖索引。表扫描发生

c# - 尝试在数据库表中插入新条目时出现 LINQ to SQL 异常