if else in select statement

(3 posts) (2 voices)

Tags:

No tags yet.

  1. Mindaugas, Member

    Hi

    Based on an IP address and country code that I fetch from a database, I want to show a corresponding country flag value for this country in a separate column?

    To do this I have used CSS images:
    select 'dbr.css', '.countryad {color: transparent; width: 25px; weight: 15px; background:url(user/flags_iso/24/ad.png) no-repeat; display:block; margin: 0 auto}';
    select 'dbr.css', '.countrygb {color: transparent; width: 25px; weight: 15px; background:url(user/flags_iso/24/gb.png) no-repeat; display:block; margin: 0 auto}';

    Then:
    SELECT ip_address AS 'IP',
    country_code as 'Country code',
    if (country_code = 'ad','countryad','countrygb') as 'Flag'
    FROM XXX_YYY

    But this allows me to only handle 2 countries. How do I go about effectively checking each row country code and based on it, show the flag in a separate column? I could not find relevant sections in the guide.

    Kind regards
    Mindaugas

  2. myDBR Team, Key Master

    You could do:

    select 'dbr.css', '.flag { width: 25px; height: 15px;  }';
    select 'dbr.colclass', 'Flag', 'flag';
    select 'dbr.cellstyle', 'Flag', 'flag_choise';
    select 'dbr.hidecolumn', 'flag_choise'; SELECT
    ip_address AS 'IP',
    country_code as 'Country code',
    null as 'Flag',
    CONCAT( "background: url('user/flags_iso/24/", country_code, ".png') no-repeat center") as 'flag_choise'
    FROM XXX_YYY;

    --
    myDBR Team

  3. Mindaugas, Member

    Awesome, this worked. I only had to add lower(country_code) in the CONCAT function, as my country codes are stored in capitals.

    Thanks
    Mindaugas


Reply

You must log in to post.