[英]How to add relationship to table rows based on column value in psql?
I have a table of patients
and a table of doctors
. 我有一张
patients
桌子和一张doctors
桌子。 I would like to assign a doctor
to every patient
. 我想为每位
patient
指派一名doctor
。 Doctors may have many patients but a Patient will belong to a single Doctor. 医生可能有很多病人,但一个病人将属于一个医生。 I would like to assign a specific doctor based on the
born_on
value of the patient
. 我想根据
patient
的born_on
值指定一名特定的医生。
Currently I am using a SET
and WHERE
clause to assign patients with a born_on
below 10
to the doctor with an id
of 1
. 目前,我正在使用
SET
和WHERE
子句将born_on
低于10
患者分配给id
为1
的医生。
UPDATE patients p
SET doctor_id = d.id
FROM doctors d
WHERE DATE_PART('year', AGE(p.born_on)) < 10
AND d.id = 1;
How could I, in minimal number of queries, assign all patients to a doctor based on their born_on
attribute. 在最少的查询中,我怎么能根据他们的
born_on
属性将所有患者分配给医生。
The conditions are below 条件如下
age - d.id
< 10 is 1
< 20 is 2
< 30 is 3
< 40 is 4
< 50 is 5
< 60 is 6
< 70 is 7
< 80 is 8
< 90 is 9
>= 100 is 10
I was wondering, if you already know the condition for update, do you have to go to the Doctor's table (meaning something like below) 我想知道,如果您已经知道更新的条件,您是否必须去看医生的桌子(意思如下)
UPDATE patients p
SET doctor_id = case DATE_PART('year', AGE(p.born_on))
when < 10 then 1
when < 20 then 2
...
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.