【发布时间】:2015-11-13 22:59:50
【问题描述】:
如果 Ptnt_id 已经存在,我想更新一行,如果不添加新行
IF EXISTS (select * from `tbl_medicalhistory` where `Ptnt_id` =0) THEN
update `tbl_medicalhistory` set `txt_tongue`= 'UPDATED' where `Ptnt_id` = 0;
ELSE
INSERT INTO `tbl_medicalhistory`(`idMed`, `Ptnt_id`, `txt_tongue`, `txt_palate`, `txt_tonsil`, `txt_lips`, `txt_floorOfMouth`, `txt_cheeks`, `txt_allergy`, `txt_HeartDisease`, `txt_BloodDyscracia`, `txt_Diabetes`, `txt_kidney`, `txt_liver`, `txt_hygiene`, `txt_others`) VALUES (20],20,"This","New","Table","","","","","","","","","","","OTHERS");
idMed 将是主键,Ptnt_id 将是外键,所以如果 Ptnt_id 已经存在,它将只更新整行,否则它将添加具有新 idMed 和 Ptnt_id 的另一行 有人可以帮我写那个QUERY/PROCEDURE吗?
【问题讨论】:
-
使用 MySQL 的
INSERT ... ON DUPLICATE KEY UPDATE语法。 Doc