MYSQL如果条件不显示,那么值为0

时间:2014-10-29 08:36:46

标签: mysql sql

我所拥有的是查询

SELECT
    mm.module_id, mm.module_name,
    pm.permission_id, pm.permission_name,
    CASE WHEN rp.permission_id is not null
        THEN false ELSE true END AS status
FROM permission_master AS pm 
LEFT OUTER JOIN role_permission AS rp
    ON pm.permission_id = rp.permission_id AND rp.RoleID = 1
LEFT OUTER JOIN module_master AS mm
    ON pm.module_id = mm.module_id
ORDER BY mm.module_id, pm.permission_id

Here is the Fiddle

它返回值,状态将根据查询中的给定条件为0和1。

我需要的是它不应该返回状态为0的颜色

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您可以在查询中添加where子句,如下所示

select mm.module_id, mm.module_name, pm.permission_id, 
pm.permission_name, 
case when rp.permission_id is not null then false else true end as status 
from permission_master as pm 
left outer join role_permission as rp on pm.permission_id = rp.permission_id 
and rp.RoleID = 1 
left outer join module_master as mm on pm.module_id = mm.module_id 
where rp.permission_id is null
order by mm.module_id, pm.permission_id

答案 1 :(得分:1)

您需要将having子句添加为
在order by子句

之前having status = 1
select
mm.module_id, 
mm.module_name, 
pm.permission_id, 
pm.permission_name, 
case 
when rp.permission_id is not null then false 
else true 
end as status 
from permission_master as pm 
left outer join role_permission as rp on pm.permission_id = rp.permission_id and rp.RoleID = 1 
left outer join module_master as mm on pm.module_id = mm.module_id 
having status = 1
order by mm.module_id, pm.permission_id

http://sqlfiddle.com/#!2/7a960/2