First of all, I have no control over the database structure, etc.
I need to use PHP to retrieve records by state and name but all the data is in multiple tables. Basically I need to try and understand how to get these two queries combined so that they do not run so slow.
First I get my record ID's in PHP. (Lets assume $query returns an array of IDs)
table_products = A bunch of products
$query = "SELECT id,name FROM table_products WHERE name = '".$name."';";
Than I need to iterate through these records (NOTE : There can be A LOT) and figure out where these IDs reside in another two tables that has the location information of where they could be at.
table_places = a table with a bunch of locations
link_table = Contains the relationships between product and location
$state = "somestate";
foreach($query as $row)
{
$query_two = "SELECT table_places.name, table_places.id, table_places.state, link_table.place_id, link_table.product_id
FROM table_places
INNER JOIN link_table
ON table_places.id = link_table.place_id
WHERE table_places.state = '".$state."' AND link_table.product_id = '".$row->id."';";
}
God I hope this made sense. I am no query guru so if I could get assistance in optimizing this to run faster, I would be grateful.