Create select with multiple fields

Asked

Viewed 264 times

4

How to create a select that lists at least 03 tables and presents a field from each table at least ?

  • 1

    Do you have any questions or do you want to learn select? I recommend looking for a tutorial, there are several in Portuguese today: http://bfy.tw/6Mr4

  • Start looking for John, I believe it helps you.

1 answer

4


In a relational database we store the data in tables. These tables relate through foreign keys (FK - Foreign Key).

Usually the primary key of one table will be present in another, which relates, in the form of foreign key.

An example is: We have 3 tables: buying, product and category.

  • buying has a product (is related to product by field produto_id, that is primary key (PK - Primary Key) of product and foreign key in the buy table);
  • product has a category (is related to category by field categoria_id, that is primary key category and foreign key in the product table);

To make a query that relates these 3 tables we have to use JOIN.

Here you can find Mysql documentation for the subject, but the concept is generic and applies to virtually all banks of dice (http://dev.mysql.com/doc/refman/5.7/en/join.html).

One SELECT to illustrate:

SELECT c.data, c.valor, p.nome, ct.descricao
  FROM tb_compra c
 INNER JOIN tb_produto p ON p.id = c.produto_id
 INNER JOIN tb_categoria ct ON ct.id = p.categoria_id
 WHERE ct.id = 12;

The query brings the fields data and valor table buying, the field nome table product and descrição table category.

I hope I’ve helped.

Browser other questions tagged

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