如何使用3列创建视图,每个视图具有不同的条件

时间:2015-12-05 15:29:46

标签: mysql sql

我想用A列(选择A条件=> 10),B(条件=> 5),C(选择C条件=> 100)制作视图。

 SELECT
(SELECT A FROM Table1 WHERE A > 10) AS 'Group1',
(SELECT B FROM Table1 WHERE B > 5) AS 'Group2',
(SELECT C FROM Table1 WHERE C > 100) AS 'Group3',

FROM Table

此查询带来了所需的结果,但在其他列条件不匹配的情况下也填充了具有空值的列。如何避免空值

2 个答案:

答案 0 :(得分:2)

如果丹尼尔库克的版本适合你,除非你想要空值而不是空值,这是一个很容易的改变:

select      case when a > 10 then a else '' end as a,
            case when b > 5 then b else '' end as b,
            case when c > 100 then a else '' end as c
from        test

SQLFidde:http://sqlfiddle.com/#!9/4c167/2

答案 1 :(得分:1)

select      case when a > 10 then a else null end as a,
            case when b > 5 then b else null end as b,
            case when c > 100 then a else null end as c
from        table