Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to query for a possible non-existent column in mysql?

Tags:

mysql

coalesce

I have a script where a user can select a field across multiple tables. It uses a Union to get all of the rows. Right now, I have a mapping array to indicate whether a specific field exists in each table, and if it doesn't, it uses "" as field_name to keep everything in check.

I was wondering if there is a way to reference a possibly non-existent column in a query, something like COALESCE so that if the column doesn't exist, instead of throwing an error, it just returns a default value.

like image 507
Anthony Avatar asked Jul 12 '11 18:07

Anthony


People also ask

How do I exclude a column in MySQL?

COLUMNS table holds all information about the columns in your MySQL tables. To exclude columns, you use the REPLACE() and GROUP_CONCAT() functions to generate the column names you wish to include in your SELECT statement later.

Does not exist in MySQL?

In MySQL, NOT EXISTS operator allows you to check non existence of any record in a subquery. The NOT EXISTS operator return true if the subquery returns zero row. The NOT EXISTS operator can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

How do you not show a column in SQL?

Teradata SQL Assistant for Microsoft Windows User Guide In the Answerset, right-click the columns to hide and select Hide Columns. To show all columns, right-click in the Answerset and select Show All Columns.

What is coalesce in MySQL?

The MySQL COALESCE() function is used for returning the first non-null value in a list of expressions. If all the values in the list evaluate to NULL, then the COALESCE() function returns NULL. The COALESCE() function accepts one parameter which is the list which can contain various values.


1 Answers

coalesce would still require a fieldname, so it wouldn't help:

... COALESCE(non_existent_field, NULL)

will still cause an error due to the field not existing.

like image 103
Marc B Avatar answered Oct 22 '22 01:10

Marc B