Return three different values between two tables that have no relationship in Mysql

Asked

Viewed 28 times

1

I have a database with 3 tables, where relationships and attributes are distributed this way: Diagrama

Precise Retornar o nome, data de nascimento e número do cartão das pessoas em ordem decrescente pela idade;

But how can I return values between tables that have no relationship? In this case the table pessoas and the table cartoes.

  • 1

    Use the accounts table for the joins, even if you won’t return any of them in the final select.

  • select pessoas.nome, pessoas.data, contas.id_conta from pessoas inner join contas on pessoas.id_pessoa = contas.pessoas_id_pessoa; Thus?

  • @Your case is solved with a simple INNER JOIN, has tried to do the following select: SELECT pessoa.nome, pessoa.data, cartao.numero FROM pessoas AS pessoa INNER JOIN contas AS conta ON conta.id_conta = pessoa.id_pessoa INNER JOIN cartoes AS cartao ON cartao.id_conta = conta.id_conta

  • worked out, @William, thanks! just a doubt, a user has two cards, but only returned one, will there be some problem?

  • @Mateus-Meros I think the modeling of the tables are not so correct so, see if this example helps you. link

  • in fact @William, I will take a look at this example and try to apply to my problem, thanks again.

Show 1 more comment

1 answer

0


Your case is solved with a simple INNER JOIN, has tried to do the following select:

SELECT 
    pessoa.nome, 
    pessoa.data, 
    cartao.numero 
FROM 
    pessoas AS pessoa 
INNER JOIN 
    contas AS conta ON conta.id_conta = pessoa.id_pessoa 
INNER JOIN 
    cartoes AS cartao ON cartao.id_conta = conta.id_conta

Browser other questions tagged

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