简体   繁体   English

根据列号在多个表中搜索

[英]Search in multiple table based on column numbers

table1 has 6 column table1有6列

table2 has 8 column table2有8列

table3 has 10 column table3有10列

table4 has 12 column table4有12列

all the tables have a common NAME, say pid. 所有表都有一个通用名称,例如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. 我想在PHP中进行查询,如果在表1中找到pid,则将其发送到PHP查询,或者在表2中进行搜索,或者如果找不到,则在表3中或在表4中进行下一步搜索。

is there any way i can differentiate before hand based on column numbers, which table to search before making the query in PHP. 有什么我可以根据列号预先区分的方法,在PHP中进行查询之前要搜索哪个表。

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 首先,您必须将array中的所有表名获取为

SHOW TABLES FROM db_name 

Now go for a foreach loop and break if count >0 现在进行foreach循环,如果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 最后在表中进行搜索查询,您从tn得到什么

If you have query on integer value. 如果查询integer数值。 Then You can maintain a Range table for that. 然后,您可以为此维护一个Range table It stores only table name like table1 and its minimum key value and maximum key value . 它仅存储table name如table1)及其minimum key valuemaximum 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找到该记录所属的表名,并从诸如table1的目标表中获取它。 Range table is small, its have 5-6 records as you said so very less time taking. Range table很小,正如您所说的,它有5-6条记录,因此花费的时间更少。 Now go to your destination table and find data. 现在转到目标表并查找数据。

Caution: This methods have its own pros & cons. 注意:此方法各有利弊。 I am just sharing my views 我只是在分享我的观点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM