我有以下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中做到這一點?
有你看到' CASE' expresion? –