balajiprasadb balajiprasadb - 1 year ago 121
MySQL Question

Mysql check if a value exists in which column

I have a table which has two columns - city and country. Given a string 'X', how to know the column name which has the value 'X'?

For example, if 'X' is new york, query should return column name City and if 'X' is USA, query should return column name Country.

Answer Source
        WHEN city = 'X' 
            THEN 'City' 
        ELSE 'Country' 
    END column_Value 
FROM [table_Name] 
WHERE city = 'X' OR country = 'X';

You can make use of the above query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download