简体   繁体   中英

MySql split an alias into multiple columns

select pd.products_name, 
GROUP_CONCAT(pag.customers_group_id SEPARATOR ',') group_id, 
pa.`options_values_price` Retail, 
GROUP_CONCAT(pag.options_values_price SEPARATOR ',') volume_and_designer


from products_attributes pa 

left join products_description pd 
on pa.products_id = pd.products_id and pd.language_id = '1' 

left join products_attributes_groups pag 
on pa.`products_attributes_id`= pag.`products_attributes_id`

where pa.products_id='225'

GROUP BY `pa`.`products_attributes_id` 

ORDER BY `pa`.`products_attributes_id` ASC

The above query return me an output like this

| products_name | group_id |   Retail |   volume_and_sdesign |
-------------------------------------------------------------
| GOLD          |    1,2   |  15      |       30,35          |
| SILVER        |    2,1   |  16      |       40,45          |
| BRONZE        |    1,2   |  17      |       50,55          |

what I want to achieve is add 2 more aliases in the table above so the last column (volume_and_sdesign) is separated into two columns (ie volume, SDesign) according to group_id column. 1 corresponds to volume and 2 correspond to SDesign.

eg

Gold has group_id (1,2)
so its volume_and_sdesign (30,35) will make new columns 
volume = 30
SDesign = 35

Silver has group_id (2,1)
so its volume_and_sdesign (40,45) will make new columns 
volume = 45
SDesign = 40

 Bronze has group_id (1,2)
so its volume_and_sdesign (50,55) will make new columns 
volume = 50
SDesign = 55

so, the above table will look like this

| products_name | group_id |   Retail |   volume_and_sdesign |  volume |  SDesign|
-------------------------------------------------------------
| GOLD          |    1,2   |  15      |       30,35          |30       | 35      |
| SILVER        |    2,1   |  16      |       40,45          |45       | 40      |
| BRONZE        |    1,2   |  17      |       50,55          |50       | 55      |

Any help will be much appreciated

You can use conditional aggregation -- that is case within an aggregation function such as max() :

select pd.products_name, 
       group_concat(pag.customers_group_id SEPARATOR ',') as group_id, 
       pa.`options_values_price` as  Retail, 
       group_concat(pag.options_values_price SEPARATOR ',') as volume_and_designer,
       max(case when group_id = 1 then pag.options_values_price end) as volume,
       max(case when group_id = 2 then pag.options_values_price end) as SDesign
from products_attributes pa left join
     products_description pd 
     on pa.products_id = pd.products_id and pd.language_id = '1' left join
     products_attributes_groups pag 
     on pa.`products_attributes_id` = pag.`products_attributes_id`
where pa.products_id='225'
group by `pa`.`products_attributes_id` 
order by `pa`.`products_attributes_id` ASC

This can be achieved using LEFT , RIGHT and SUBSTRING_INDEX .

Try this:

select
    pd.products_name, 
    GROUP_CONCAT(pag.customers_group_id SEPARATOR ',') group_id, 
    pa.`options_values_price` Retail, 
    GROUP_CONCAT(pag.options_values_price SEPARATOR ',') volume_and_designer,
    IF(LEFT(group_id,1) = 1, SUBSTRING_INDEX(volume_and_designer, ',', 1), SUBSTRING_INDEX(volume_and_designer, ',', -1)) as volume,
    IF(RIGHT(group_id,1) = 1, SUBSTRING_INDEX(volume_and_designer, ',', 1), SUBSTRING_INDEX(volume_and_designer, ',', -1)) as SDesign
from products_attributes pa 
left join products_description pd 
on pa.products_id = pd.products_id and pd.language_id = '1' 
left join products_attributes_groups pag 
on pa.`products_attributes_id`= pag.`products_attributes_id`
where pa.products_id='225'
GROUP BY `pa`.`products_attributes_id` 
ORDER BY `pa`.`products_attributes_id` ASC

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