我正在使用以下脚本为表“患者”生成模拟数据:
WITH vars (tenantId, patientCount) as (
values (5, 30)
)
INSERT INTO patient (tenant_id, id, name)
SELECT
tenantId AS tenant_id,
gs AS id,
CONCAT('Patient_', row_number() OVER ()) AS name
FROM vars, generate_series(0, 0 + patientCount) AS gs;
结果:
pk | tenant_id | id | name
------+-----------+----+-----------
3811 | 5 | 0 | Patient_1
3812 | 5 | 1 | Patient_2
3813 | 5 | 2 | Patient_3
3814 | 5 | 3 | Patient_4
3815 | 5 | 4 | Patient_5
接下来我想用这些列将数据生成到“治疗”表中:
pk (autogenerated)
id (unique)
patient_pk (from the patient table above)
我应该采取什么方法来完成这项任务?
你可以这样做,将数据生成到治疗表中
insert into treatment (id, patient_pk)
select id::int8, 3811+round(random() * 31)::int8 as patient_pk
from generate_series(1, 50) as id
where 3811+round(random() * 31)::int8 in (select pk from patient);