Reputation:
How can I make this two queries in one ?
$query = "SELECT * FROM `amb_prod` WHERE idAmbiente='".$ambId."';";
$result_set = mysql_query($query);
while($member = mysql_fetch_array($result_set)){
$query2 = "SELECT * FROM `produto`, `pt` WHERE
produto.refPT = pt.ref AND
produto.refPT = $member['idProduto'] ;";
$result_set2 = mysql_query($query2);
}
I have have tried this but it didn't work..
$query = "SELECT * FROM `produto`, `pt` WHERE
produto.refPT = pt.ref AND
produto.refPT = (SELECT `idProduto` FROM `amb_prod` WHERE idAmbiente='".$ambId.");";
Upvotes: 1
Views: 443
Reputation: 407
SELECT * FROM produto
, pt
, amb_prod
WHERE
produto.refPT = pt.ref AND
produto.refPT = amb_prod.idProduto
AND amb_prod.idAmbiente='".$ambId."' ;
Based on the data, you may have to use distinct in the select clause
Upvotes: 0
Reputation: 28736
With a join instead of subquery:
$query = "SELECT pr.*, pt.*
FROM amb_prod ap
JOIN producto pr ON (ap.idProduto = pr.refPT)
JOIN pt ON (pr.refPT = pt.ref)
WHERE idAmbiente='${ambId}'";
Upvotes: 2
Reputation: 121424
You cannot have two cursors open in the same connection the same time. You need to open a second connection. I would strongly advise against that though; issuing a query for every row read would be a bit slow. If I were you, I would do this:
SELECT pr.*, pt.*
FROM "produto" pr, "pt" pt, amb_prod ap
WHERE produto.refPT = pt.ref
AND ap.idAmbiente = $ambId
AND produto.refPT = ap.idProduto
Ideally, you would convert this to a parametrized query, for security, maintainabilty and performance resons. I'm not sure how it is done in PHP but the MySQLi_STMT class looks like a good starting point:
Upvotes: 0
Reputation: 25011
This should work:
$query = "SELECT * FROM `produto`, `pt` WHERE
produto.refPT = pt.ref AND
produto.refPT IN (SELECT `idProduto` FROM `amb_prod` WHERE idAmbiente='".$ambId.");";
I'm not sure about the table structure, but a join may work as well.
Upvotes: 2