Query which table a given value is in
P粉727416639
2023-08-10 14:56:55
<p>I need some help. </p>
<p>In my database I have <strong>several tables</strong> containing product details, each product has a unique ID. </p>
<p>Now I have a given product ID - <strong>How do I get the table name of which table this product is stored in</strong> since I have different tables in my database? How can I store the table name in a PHP variable so that I can reuse it in another select statement? </p>
<p>I searched on stackoverflow but only found answers about getting all table names of a database or column names. I'm looking for a simple solution in mysql/pdo select statement. </p>
<p>My family is very busy</p>
To search within a fixed set of tables, you can combine individual queries using the
UNION
clause and then add a hardcoded string to identify the source:You need to ensure that each column in the result set has the same data type, and you can use the first statement to set a normalized column name for the result set.