MySQL Join a Table if Exists Question
I have a DB which has a table called config
. T开发者_如何学JAVAhis table could possibly have a config_cstm
table that is related by the id to 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 would go about checking for the existence of an existing table in PHP. I was wanting to do this all in one MySQL statement though.
Any suggestions?
If you're using MySQL 5 or better, you can use CASE for that sort of conditional functionality.
IMHO, however, if you're using php, you should do it in php rather than trying to shoehorn it all into a SQL query. It's usually not very efficient.
You're talking about "my DB", but in my experience, generally in a given DB, a table either exists or doesn't exist. Are you trying to apply the same query to a number of parallel DBs?
If so I would think that other, more major structural differences might arise, and as Satanic Puppy says, you'd be miles better off constructing your query in PHP or another pre-SQL language, rather than trying to prepare a query that will fit all molds.
精彩评论