简体   繁体   中英

how to count the same value twice in query

the result am getting from this query below is obviously one file, this one file has just rur in whataver column, I want to have two entries with the same file, i am interested in having that counted twice. filename size type(this is an enum) 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%'

my result is

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

i want to get

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

at the same time, my file has size and usually I want to know how many MB do I have with rur, and how many with way. I want to create a pivot table summary thats why I want them all in one view/table. so i want to also get the size of 10mb rur and 10 mb way if possible. otherwise i can avoid counting the size that way

I want to get the result as i have 30% rur 20% way and xx% null

Just use an 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: unless MySQL does not support it. Not sure, but my guess is that it does, it's ANSI SQL after all. PPS: it does .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM