Vyshnavi Samudrala Vyshnavi Samudrala - 4 months ago 8
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

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
Comments