table1 has 6 column
table2 has 8 columntable3 has 10 columntable4 has 12 columnall the tables have a common NAME, say pid. i want to make a query in PHP which says, if, pid is found in table1, send the data to PHP query or else search in table2, or if not found, search in table3 or next in table4.
is there any way i can differentiate before hand based on column numbers, which table to search before making the query in PHP.
If you have query on integer
value. Then You can maintain a Range table
for that. It stores only table name
like table1 and its minimum key value
and maximum key value
. If any query on that integer value then we can found the table name from which this record belong by using range table
and get it from target table like table1. Range table
is small, its have 5-6 records as you said so very less time taking. Now go to your destination table and find data.
Caution: This methods have its own pros & cons. I am just sharing my views
Since you did not attached code , logical solution is mentioned here. You could do proceed by checking if column exist then search in that
At very first , you will have to get all table names in array as
SHOW TABLES FROM db_name
Now go for a foreach loop and break if count >0
SELECT count(*) as c,TABLE_NAME as tn
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name' // your db name
AND TABLE_NAME = 'table_name' // from loop get it
AND COLUMN_NAME = 'pid' // column name
Now check c in php if >0 return tn and then break
Finally do your search query in table what you get from tn