Select data from two tables to display in one column?

Asked

Viewed 514 times

4

Consider the tables for customer registration :

Table Person

|ID|NOME|TIPO|EMAIL|

Table Person Physical

|ID|CPF|

Person Juridical Table

|ID| CNPJ| inscricao_municipal| inscricao_estadual|

My goal is to consult the data of all people ,at first I used the following instruction :

SELECT cnpj , cpf, nome , email
from pessoa
left join pessoa_juridica on pessoa.id = pessoa_juridica.id
left join pessoa_fisica on pessoa.id   = pessoa_fisica.id

Upshot :

| CPF | CNPJ | nome | email | inscricao_estadual |inscrição_municipal |

how can I display the CNPJ and CPF in the same column ?

Example :

|CPF / CNPJ | nome | email | inscricao_estadual | inscrição_municipal |

1 answer

7


You can use the function COALESCE.

COALESCE(pessoa_juridica.CNPJ, pessoa_fisica.CPF)
  • Thank you very much ! worked here !

  • Interesting this Coalesce +1

Browser other questions tagged

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