Jump to content

[SOLVED] Best way to query more than one table in a single statement?


suttercain

Recommended Posts

I have about 6 tables all with the same column names but seperated by year. Table names are 1994data, 1995data, 1996data etc.

 

Is there an efficient way to query through each of these table when doing a search? Is table join the only way?

 

<?php
$engfam = mysql_real_escape_string($_POST['efn']);
	$sql = mysql_query("SELECT * FROM 1994data WHERE engfam ='".$engfam."'") or die(mysql_error());
?>

 

Thanks

 

SC

Link to comment
Share on other sites

$query = "SELECT * FROM 1994data, 1995data, 1996data" .
            " WHERE (" .
               " 1994data.engfam = '$engfam' OR " .
               " 1995data.engfam = '$engfam' OR " . 
               " 1996data.engfam = '$engfam'" .
            ")";

Link to comment
Share on other sites

Figured it out using an array and for loop:

 

$year_arr = array("1994","1995","1996");
    $arr_size = count($year_arr);


	if (isset($_POST['efn'])) {
	$engfam = mysql_real_escape_string($_POST['efn']);
	$flag = 0;
              for ($i = 0; $i < $arr_size; $i++) 
              {
                //SQL Statement
                $sql = mysql_query("SELECT * FROM " .$year_arr[$i]. "data WHERE ENGFAM = '$engfam'") or die(mysql_error());
		  

while ($row = mysql_fetch_array($sql)) {
echo "Engine Family Name: " . $row['engfam'] ."<br>";
		}
	}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.