3
good afternoon, how can I make an appointment where I have in my field idProduto
several ids
of product separated by comma and list each of its respective.
example: in my idProduto
I have the ids 1,2,6
I want that in my consultation he returns me with the inner join
the data of the respective products containing those ids
how can I do this?
"SELECT p.id, p.idProduto, p.total, p.rastreio, p.envio, p.qtd, p.status, p.data, c.nome AS nomeCliente, c.email, c.telefone, c.cpf, c.cep, c.nResidencial, prod.nome FROM pedidos p INNER JOIN clientes c ON p.id_cliente = c.id INNER JOIN produtos prod ON prod.id = p.idProduto WHERE p.id = '$idPedido' "
Like this the contents of this variable
$idPedido
?– R.Santos
intact this content would be to pick up the id of the respective request for example let’s say the
id
of the request be 1 then it will return me only the data of this respectiveid
, and within that request with theid=1
I have several products purchased. so this$idPedido
and more to know the data of the respective selected order– bryant costa
Ever tried to use
WHERE p.id IN ('$idPedido')
instead of justWHERE p.id = '$idPedido'
?– Gustavo Araújo
But in case of more than one id like this variable gets? (1,2,3,4,5) ?
– R.Santos
not only will there be 1 id in this variable which will exist more than one id and in the idProduct
– bryant costa
I think you’ve got the wrong intention
$idPedido
this correct what I want and list the products that are in the fieldidProduto
for example I put in select Prod.name I want the name of each product that contains theid = idProduto
In the example I have theidProduto
with 3ids
1,2,6 yet it shows me 3 names that are in theid 1,2,6
tableproduto
– bryant costa
Your you put
SELECT p.id, p.idProduto, p.total, p.rastreio, p.envio, p.qtd, p.status, p.data, c.nome AS nomeCliente, c.email, c.telefone, c.cpf, c.cep, c.nResidencial, prod.nome FROM pedidos p INNER JOIN clientes c ON p.id_cliente = c.id INNER JOIN produtos prod ON prod.id = p.idProduto WHERE p.id = '3'
works?– R.Santos
if I put in the
p.id
that and the order id it will return me all the data where thep.id = 3
up to ai blz so I want it to return all the names ofprod.nome
– bryant costa
it only returns me the first name and as I have 3
ids
he would have to return me the 3 names. ex:p.idProduto
contains the followingid 1,2,3
blz when I do theinner join
prod.id = p.idProduto
i speak every id of theproduto
it has to be the same as the one in my columnidProduto
or be it will look for the followingids
on the tableproduto
1,2,3
and bring me the names that are in thoseids
but he brings me only the first name I want him to return to me 3 name the name ofid = 1
, the ofid = 2
, and that ofid = 3
, not only that ofid = 1
– bryant costa
INNER JOIN produtos prod ON prod.id IN (p.idProduto)
The above instruction could work since the values of thep.idProduto
are separated by comma, but as it returns a string it reads the number up to the first comma returning only one result. What I would do is 2 queries one relating the order with the client dai vc would then have the requests, using PHP would inject in another SQL statement making the SELECT in the product table the id’s using theWHERE id IN ()
.– Gustavo Araújo
I tried to make the
idProduto
in a variable and made the following querySELECT nome FROM produto WHERE id IN($listadeProduto)
however he brought me only the first result again, he did not go through my id he only caught the first again– bryant costa
Do this, take the result with the product id’s and one explodes:
$idProdutos = explode(',',$idProdutos);
Now vc has transformed the string into an array, each id is now separated, now just vc inside the parentheses of theIN
concatenate and make a foreach:(. foreach($idProdutos as $idProduto): echo $idProduto","; endforeach;.)
– Gustavo Araújo