0

table1 has 6 column

table2 has 8 columntable3 has 10 columntable4 has 12 column

all 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.

MANAUWER RAZA
  • 29
  • 1
  • 10
  • If you want to search through your whole database, for table with the specific column name, then this could be of help. http://stackoverflow.com/questions/193780/how-to-find-all-the-tables-in-mysql-with-specific-column-names-in-them – MaK Oct 01 '15 at 07:16

2 Answers2

1

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

Rohit Kumar
  • 1,948
  • 1
  • 11
  • 16
  • please, see to this. http://stackoverflow.com/questions/32884535/search-record-in-multiple-tables-and-print-output-differently-in-mysql-from-php – MANAUWER RAZA Oct 01 '15 at 10:32
0

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

Hitesh Mundra
  • 1,538
  • 1
  • 9
  • 13