如何从 table select 行,但如果它不存在于 table,select 来自不同的 table?
How to select row from table but if it doesn't exist in that table, select from different table?
这可能是一个非常简单的问题,我忽略了但我有这个查询:
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
(客户 ID 会有所不同,我正在使用节点 js 脚本获取一堆不同的客户 ID 以查明他们是否是会计师)
如果没有结果,我想在名为 deleted_users
的 table 中搜索相同的客户 ID,即:
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
有没有办法在 Postgresql 中做到这一点?
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)
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
union all
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
and not exists
(SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT')
或者,如果您有兴趣了解用户 "is (or was)" 会计师:
(table 用户在哪有关系吗?同意吗?)
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
union
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
这可能是一个非常简单的问题,我忽略了但我有这个查询:
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
(客户 ID 会有所不同,我正在使用节点 js 脚本获取一堆不同的客户 ID 以查明他们是否是会计师)
如果没有结果,我想在名为 deleted_users
的 table 中搜索相同的客户 ID,即:
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
有没有办法在 Postgresql 中做到这一点?
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)
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
union all
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
and not exists
(SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT')
或者,如果您有兴趣了解用户 "is (or was)" 会计师: (table 用户在哪有关系吗?同意吗?)
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
union
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'