我有以下sql语句:
SELECT p.id, p.first_name, p.last_name, a.admitted_at,
a.discharged_at, c.name as client_name, p.receive_reminders,
p.opt_in_to_receive_reminders, u.phone_number, p.status,
c.id as client_id, p.mrn
FROM patients p
LEFT JOIN admissions a ON p.last_admission_id = a.id
INNER JOIN facilities f ON f.id = p.facility_id
INNER JOIN clients c ON c.id = f.client_id
LEFT OUTER JOIN users u ON p.user_id = u.id;
我有三列:
p.opt_in_to_receive_reminders - boolean
u.phone_number - string
p.receive_reminders - boolean
我想将这3列的结果合并到一个虚拟列 - messaging
。例如,如果p.opt_in_to_receive_reminders
为真并且u.phone_number
不为空且p.receive_reminders
为真,则此列将具有真值,在其他情况下,它将返回false。
我如何在PostgreSQL中执行此操作?
答案 0 :(得分:3)
请写下:
p.opt_in_to_receive_reminders为true,如果u.phone_number不为空且p.receive_reminders为真
作为
select p.opt_in_to_receive_reminders and u.phone_number is not null and p.receive_reminders
-- or
select p.opt_in_to_receive_reminders and u.phone_number <> '' and p.receive_reminders
-- or
select p.opt_in_to_receive_reminders and coalesce(u.phone_number, '') is null and p.receive_reminders