Select deleted a record and searching for others with the same code

Asked

Viewed 39 times

0

The product code I pass to sql via php.

For example $cd_product = 2; I would like to list everything but the product record (cd_product != 2).

So far so good.

However I would also like only the records of the table (product table) with the code of the category of this product (2).

SELECT * 
FROM tabela_produto 
WHERE cd_produto != 2  AND cd_categoria = (como saber o código da categoria desse produto que foi eliminada)
  • The cd_categoria is in the same table as the cd_produto?

  • Yes in the same table.

  • select * FROM tabela_produto WHERE cd_produto != 2 AND cd_categoria = (select cd_categoria where cd_produto = 2 ). Try this, and tell me if that’s what you really want

  • Right! Thank you.

1 answer

2


Thus:

SELECT * FROM tabela_produto WHERE cd_produto != 2 AND 
cd_categoria = (select cd_categoria from tabela_produto where cd_produto = 2 )
  • 1

    That’s right! Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.