Search code examples
postgresqlknex.js

how to group two columns in one


i have three tables

customer, people, companies

tables people and companies have a "customerID"

table people have "cpf" and table companies have "cnpj"

i need to query

customerID, (cpf OR cnpj)


Solution

  • Using a union should do the trick. Give this a try:

    (SELECT customerID, cpf as cpfORcnpj FROM people )
    UNION 
    (SELECT customerID, cnpj as cpfORcnpj FROM companies)