Select to bring specific record of an id

2

I would like to know how to get a specific record of a fk, I have the hosting table and the consumption (with codHospedagem ). When I select some hosting, I click on the consumption button of my consumption screen.

I need to bring into consumption the consumption records of that hosting that I select, because my select is not working, it only brings the empty line. Below the code:

SELECT c.codConsumo as Consumo,c.codHospedagem as Hospedagem , 
p.nomeProduto AS Produto, 
c.quantidade, c.valorConsumo, c.status 
FROM consumo c   
INNER JOIN  produto AS p ON p.codProduto = c.codProduto                   
INNER JOIN hospedagem AS H ON H.codHospedagem = C.codHospedagem                 
WHERE H.codHospedagem = c.codConsumo
ORDER BY c.codConsumo ; 
    
asked by anonymous 19.05.2016 / 23:27

1 answer

2

Check the syntax of where because you are filtering the hosting by the consumption code, not the hosting related to the consumption.

Exemplifying:

In your code, H.codHospedagem is related to C.CodHospedagem , then your where should contain a reference to hosting and not to consumption .

SELECT c.codConsumo as Consumo,c.codHospedagem as Hospedagem , 
p.nomeProduto AS Produto, 
c.quantidade, c.valorConsumo, c.status 
FROM consumo c   
INNER JOIN  produto AS p ON p.codProduto = c.codProduto                   
INNER JOIN hospedagem AS H ON H.codHospedagem = C.codHospedagem                 
WHERE H.codHospedagem = /*c.codConsumo*/
ORDER BY c.codConsumo ; 
    
19.05.2016 / 23:48