FOREIGN KEY Mysql PHP

Asked

Viewed 46 times

-1

I’m a beginner in this world and I have a question.

How do I access only a single row of an SQL table by ID?

I have a "company" table that has a foreign key with the id of another table: table "orders". That is, in the system each company has its due requests.

When the site’s Adm accesses your page, it has access to those companies and the orders that were placed by them.

How do I, when Adm enters the system and click on the page of a specific company, only the orders of that company that was accessed appear? How do I add/access data from a single row only in the Mysql table by id??

I am grateful!

  • 1

    It wouldn’t be enough to do SELECT ... FROM pedidos WHERE id_empresa = 1 (replacing 1 with the id in question)?

1 answer

0

Taking into account the information and that is a simple relationship, first you would need to "join" the "company" and "orders" tables. This is done through the inner join, where you will be referencing one table to another through an attribute they share with each other (in this case it is the foreign key). If you do the Join:

SELECT * FROM pedidos 
inner join empresa on empresa.idempresa = pedidos.idempresa

you will be able to return all the requested table data of a certain company, to which you would pass her id in the clause WHERE of your consultation. In a general way:

SELECT * FROM pedidos 
inner join empresa on empresa.idempresa = pedidos.idempresa 
WHERE empresa.idempresa = 1

this example query returns all company orders whose id is 1.

  • Thank you for the reply, I will try here!

Browser other questions tagged

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