Consult in 2 table

Asked

Viewed 62 times

0

I do not know how to consult in two tables of the same bank.

tb_chamados     
id           = 1                    | 2
fkidempresa  = 47                   | 33
resumo       = descrição do chamado | descrição do chamado

tb_empresas
id           = 47                   | 33
nome         = Nome da empresa      | Nome da empresa

I make an appointment with select * from chamados but the result comes fkidempresa, I would like you to return the name that is in the companies table and not the fkidempresa.

  • Thiago, this question already exists, I will mark it as duplicate. The following is the question on the link: https://answall.com/questions/272290/como-fazer-inner-join-mysql

1 answer

4

SELECT (campos)
FROM tabela1 t1 -- t1 é um alias, como se fosse um 'apelido', poderia ser qualquer nome
INNER JOIN tabela2 t2 on t1.campoReferenciado = t2.campoReferenciado

In your case it would be:

select * 
from tb_chamados c
inner join tb_empresas e on e.id = c.fkidempresa

To understand the types of joins see this link


And a good illustration to represent the types of join: inserir a descrição da imagem aqui

  • the company name does not appear. Instead of putting the * I had to put all the field described. " select *, tb_empresas.name from tb_chamados c Inner Join tb_empresas e on e.id = c.fkidempresa" <- error. But I managed to solve.

  • Error? What error? The * serves to select all fields of all consulted tables. If you want all fields of a given table, it would be c.* (considering c the alias of one of the tables)

  • : 'No value has been provided for one or more required parameters.'

  • select c.*, tb_empresas.name from tb_chamados c Inner Join tb_empresas e on e.id = c.fkidempresa <- same error. But I fixed it by typing in all the field names.

Browser other questions tagged

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