简体   繁体   中英

Use mysql and php to find the name of the table storing the data

I have a db with 8 tables which intend to search for an id number by joining them but i would like to retrieve the name of the table to use in a php along the lines of...

if($tablename == 'accounts'){
     echo 'value found in accounts';
}

What would the mysql query be?

(I don't need help with the joining etc, just the bit that would get the table name and how to store it in $tablename)

Thanks

SELECT table_name FROM INFORMATION_SCHEMA.TABLES
  WHERE table_schema = 'db_name'

19.12. The INFORMATION_SCHEMA TABLES Table

How can you search for data in a table if you don't know the table name in the first place?

So I suppose that you already know the table names, and don't need things like SHOW TABLES ; that you have eg a WHERE of the form

WHERE (table1.field LIKE '%search%' OR table2.field2 LIKE '%search%' OR ...)

and once retrieved a row, you know it matches, but you don't know where the match did occur. And running eight straight queries instead of one JOINed isn't an option.

If this is the case, you'll have to supplement your SELECTed fields with a flag to tell you what you need:

SELECT ..., CASE
    WHEN accounts.field1 LIKE '%search%' THEN 'accounts'
    WHEN customers.name LIKE '%search%' THEN 'customers'
    ...
    END AS wtf FROM <rest of your query>

Then in PHP when retrieving the row, you will just use the field:

print "Value found in {$row['wtf']}";

'SHOW TABLES' afair, is it still used on mysql? :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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