Say I am not sure if table t
in database d
has a column x
or not. Perhaps this is because some of the databases the application uses have been upgraded and some have not, so some have t.x
and some don't.
Now say I just need a simple query to get a row from d.t
and I want the value of d.t.x
if the column exists and some default if not.
I know how to do it with two queries in which the first looks to see if column x
exists and the app adjusts the second query accordingly. But can I move this logic into the SQL and do it in one query? If there were a COLUMN_EXISTS()
function then the query would be:
SELECT s, t, IF(COLUMN_EXISTS(x), x, 'NO_COL_x') AS x FROM d.t ...;