Vyshnavi Samudrala Vyshnavi Samudrala - 1 year ago 65
SQL Question

Table names in an array and search within them

I need to search for data in different tables which are stored in an array, how can I make the query?

I have an array tables_array in which I have different table names I need to search for data with some 'where' conditions within these tables

Answer Source

If you want to search to all tables do

$sql = "";
$table_array=array('table_name_1', 'table_name_2', 'table_name_3');
foreach($table_array as $v) {
  if($sql !== "") {
    $sql .= " union";
  $sql .= "select * from `$v` where `field_search` = '$search_value'";

echo $sql; //test your sql
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download