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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With