Search code examples
postgresqlpostgresql-9.3

How to select row from table but if it doesn't exist in that table, select from different table?


This may be a really simple problem that I'm over-looking but i have this query:

SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT' 

(the customer id will vary, I'm using a node js script to grab a bunch of different customer ids to find out if they're accountants or not)

If there is no result, I want to search in a table called deleted_users for the same customer id ie:

SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT' 

Is there a way to do this within Postgresql?


Solution

  • SELECT coalesce(u.is_accountant, d.is_accountant)
    from deleted_users d
    full outer join users u on u.id = d.id
    where 'cus_4znUZe3lAy26FT' in (d.customer_id, u.customer_id)