balajiprasadb balajiprasadb - 8 months ago 47
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.

        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.