MySQL joins table if question exists

I have a database with a table config

. This table can be a table config_cstm

associated by an identifier with the config table.

I was wondering - is there a way to dynamically check for the existence of this table in one simple select statement?

In other words, something like:

select * 
from   config 
(IF EXISTS config_cstm THEN 
           LEFT OUTER JOIN config_cstm ON config.id = config_cstm.id_c)

      

I know how I will check for the existence of an existing table in PHP. I would like to do it all in one MySQL statement.

Any suggestions?

+2


a source to share


2 answers


If you are using MySQL 5 or higher, you can use CASE for this kind of conditional functions.



IMHO, however, if you are using php you should do it in php instead of trying to train it all in a SQL query. This is usually not very effective.

+1


a source


You are talking about "my DB", but in my experience, usually in a given DB, the table exists or does not exist. Are you trying to apply the same query to multiple parallel DBs?



If so, I think there may be other, more serious structural differences, and as Satanic Puppy says, you would be much better off constructing your query in PHP or another pre-SQL language rather than trying to prepare a query that will fit all forms.

-1


a source







All Articles