首页 文章

为sql查询搜索更好的解决方案

提问于
浏览
1

我有下表:

| col1 (varchar) | col2(varchar) | col3(varchar) | col4(bool) | col5(bool) | col6(bool) | col7(bool) |
|----------------|---------------|---------------|------------|------------|------------|------------|
| a              | a             | a             | true       | true       | false      | false      |
| b              | b             | b             | true       | true       | true       | true       |
| c              | c             | c             | false      | false      | true       | false      |

列4 5 6和7指的是类型(type1,type2,type3,type4)

因此,如果col4为true,我必须将type1分配给元组(col1,col2,col3),但是一个元组可以有多个类型,所以使用上面的表格结果将是这样的

| col1 (varchar) | col2(varchar) | col3(varchar) | type    |
|----------------|---------------|---------------|---------|
| a              | a             | a             | "type1" |
| a              | a             | a             | "type2" |
| b              | b             | b             | "type1" |
| b              | b             | b             | "type2" |
| b              | b             | b             | "type3" |
| b              | b             | b             | "type4" |
| c              | c             | c             | "type3" |

我可以使用4个查询并使用 CASE WHEN

select col1,col2,col3, CASE col4 WHEN true THEN "type1" END from ... where ...;

select col1,col2,col3, CASE col5 WHEN true THEN "type2" END from ... where ...;

select col1,col2,col3, CASE col6 WHEN true THEN "type3" END from ... where ...;

select col1,col2,col3, CASE col7 WHEN true THEN "type4" END from ... where ...;

并将结果连接到一个表中,但这是一个昂贵的解决方案(我必须编写所有查询) .

有更好的解决方案吗?

2 回答

  • 0

    从布尔列创建一个数组,并且不要跳过空值:

    select col1, col2, col3, type
    from (
        select 
            col1, col2, col3, unnest(array[
                case when col4 then 'type1' end,
                case when col5 then 'type2' end,
                case when col6 then 'type3' end,
                case when col7 then 'type4' end]) as type
        from my_table
        ) s
    where type is not null;
    
     col1 | col2 | col3 | type  
    ------+------+------+-------
     a    | a    | a    | type1
     a    | a    | a    | type2
     b    | b    | b    | type1
     b    | b    | b    | type2
     b    | b    | b    | type3
     b    | b    | b    | type4
     c    | c    | c    | type3
    (7 rows)
    

    查看SqlFiddle.中的在线演示

    没有数组的解决方案:

    select *
    from (
        select col1, col2, col3, case when col4 then 'type1' end as type from my_table
        union all
        select col1, col2, col3, case when col5 then 'type2' end as type from my_table
        union all
        select col1, col2, col3, case when col6 then 'type3' end as type from my_table
        union all
        select col1, col2, col3, case when col7 then 'type4' end as type from my_table
        ) s
    where type is not null
    order by col1, col2, col3, type
    
  • 3

    您可以使用一个 case 表达式执行此操作:

    select col1, col2, col3,
           (CASE WHEN col4 THEN 'type1' 
                 WHEN col5 THEN 'type2' 
                 WHEN col6 THEN 'type3' 
                 WHEN col7 THEN 'type4' 
            END) as type
    from ...
    where ...;
    

    注意使用单引号 . 这些是SQL中的标准字符串分隔符 .

相关问题