1

从下面的查询中得到的结果显然是一个文件,这个文件刚刚在 whataver 列中出现,我想在同一个文件中有两个条目,我有兴趣将其计算两次。文件名大小类型(这是一个枚举)ABC 10mb rur,way

        select filename,,size,type, (case when type like '%rur%' then 'rurroad' when type like '%way%' then 'hway' end) as whatever 
        from files as f,metadata as v where f.id = v.id 
        and filename like 'ANC'
        and type like '%rur%'

我的结果是

         filename   size    type(this is an enum)       whatever
         ABC    10 mb     rur,way                          rur

我想得到

         filename   size    type(this is an enum)       whatever
         ABC    10 mb     rur,way                          rur
         ABC    10 mb     rur,way                          way

同时,我的文件有大小,通常我想知道 r 有多少 MB,以及 way 有多少。我想创建一个数据透视表摘要,这就是为什么我希望它们都在一个视图/表中。所以如果可能的话,我还想获得 10mb rur 和 10mb way 的大小。否则我可以避免那样计算尺寸

我想得到结果,因为我有 30% rur 20% way 和 xx% null

4

1 回答 1

0

只需使用UNION

select filename, size, type, (case when type like '%rur%' then 'rurroad' when type like '%way%' then 'hway' end) as whatever 
    from files as f, metadata as v where f.id = v.id 
    and filename like 'ANC'
    and type like '%rur%'

union all

select filename, size, type, (case when type like '%rur%' then 'rurroad' when type like '%way%' then 'hway' end) as whatever 
    from files as f, metadata as v where f.id = v.id 
    and filename like 'ANC'
    and type like '%way%' -- here we select %way% records - records with "rur,way" will appear in both queries.

PS:除非 MySQL 不支持。不确定,但我猜它确实如此,毕竟它是 ANSI SQL。PPS:确实如此

于 2013-10-04T15:55:24.803 回答