1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[SQL] How to replace multiple columns in one SQL query?

Discussão em 'Outras Linguagens' iniciado por Stack, Setembro 13, 2021.

  1. Stack

    Stack Membro Participativo

    I have 2 tables: country and trip. A trip can have up to 3 country codes.

    country table

    country_code​
    country_name​
    FRA​
    FRANCE​
    IRL​
    IRELAND​
    JPN​
    JAPAN​
    MAR​
    MOROCCO​
    NZL​
    NEW ZELAND​
    trip table

    trip_id country_code country_code2 country_code3
    1 FRA IRL JPN
    2 MAR NZL

    My goal is to have country names displayed on the trip table instead of country codes. I succeed to have only 1 country code replaced, thanks to the left join clause. I would like to have up to 3 country names displayed per row.

    SELECT trip_id, country_name
    FROM trip
    left join
    country
    on country_code = country_name

    The ACTUAL output of the trip table

    trip_id country_name
    1 FRANCE
    2 MOROCCO

    Is there a way to replace each country code with its corresponding country name?

    The EXPECTED output of the trip table

    trip_id country_name country_name2 country_name3
    1 FRANCE IRELAND JAPAN
    2 MOROCCO NEW ZELAND

    Thank you!

    Continue reading...

Compartilhe esta Página