gpt4 book ai didi

mysql - 使用单个 Replace 语句更新表

转载 作者:行者123 更新时间:2023-11-29 13:26:34 24 4
gpt4 key购买 nike

我正在尝试使用已更改的表来更新表。插入内容是:

-- services ------------------
insert into upd_services values
( 100, 'Consult- no charge', 0.00)
, ( 101, 'Routine Exam- Bird', 35.00)
, ( 102, 'Followup Exam-Bird', 32.00)
, ( 103, 'Routine Exam-Feline', 50.00)
, ( 104, 'Followup Exam-Feline', 45.00)
, ( 105, 'Routine Exam-Reptile', 25.00)
, ( 106, 'Followup Exam-Reptile', 23.00)
, ( 107, 'Rabies_V-2008 SFall-Canine', 15.00)
, ( 108, 'Rabies_V-2008 SFall-Feline', 15.00)
, ( 109, 'Rabies_Ve-2008 Spring-Canine', 25.00)
, ( 110, 'Rabies_V-2008 Spring-Feline', 25.00)
, ( 111, 'Rabies_V-Rodent', 20.00)
, ( 112, 'Rabies_V-2008 Winter-Canine', 15.00)
, ( 118, 'First Feline PCR', 20.25)
, ( 119, 'Second Feline PCR', 20.25)
, ( 120, 'Third Feline PCR', 20.25)
, ( 121, 'Flu Rhino Vacc', 26.00)
, ( 122, 'Scaly Mite', 35.00)
, ( 123, 'Intestinal Parasite Screen', 26.00)
, ( 124, 'Tick Removal', 15.00)
, ( 125, 'Behaviour Modification', 75.00)
, ( 126, 'Vitamin E- Concentrated', 30.00)
, ( 127, 'Sedative-Feline', 25.00)
, ( 128, 'Flea Treatment- Small Animal', 35.00)
, ( 129, 'Flea Treatment- Large Animal', 50.00)
, ( 143, 'Rabies_V-2010 SFall-Canine', 15.00)
, ( 144, 'Rabies_V-2010 SFall-Feline', 15.00)
, ( 145, 'Rabies_V-2010 Spring-Canine', 25.00)
, ( 146, 'Rabies_V-2010 Spring-Feline', 25.00)
, ( 147, 'Rabies_V-2010 Winter-Canine', 15.00)
;

insert into upd_services_changes values

( 128, 'Flea Treatment- Small Animal', 45.00)
, ( 111, '', 35.25)
, ( 122, 'Scaly Mite Powder', null)
, ( 138, 'Flu Rhino Vaccine enhanced', 125.00)
, ( 124, null, 25.95)
, ( 129, 'Flea Treatment- Large Animal', 65.00)
, ( 136, 'Hazardous Materials Disposal', 10.50)
, ( 126, 'Vitamin E- Concentrated', 45.00)
, ( 106, '', 30.00)
, ( 105, Null, 35.00);

我的目标是仅对现有 ID 进行更新,我已经做到了。还有我如果服务表中的描述为空,则不需要更改它;如果价格字段为空,则不需要更改它,但我被困住了。这是我到目前为止所拥有的:

replace into upd_services 
select * from upd_services_changes
where .....

REPLACE INTO upd_services (srv_id, srv_desc, srv_list_price)
values
( 128, 'Flea Treatment- Small Animal', 45.00)
, ( 111, '', 35.25)
, ( 122, 'Scaly Mite Powder', null)
, ( 124, null, 25.95)
, ( 129, 'Flea Treatment- Large Animal', 65.00)
, ( 126, 'Vitamin E- Concentrated', 45.00)
, ( 106, '', 30.00)
, ( 105, Null, 35.00);

最佳答案

由于您的目标是...仅对现有 ID 进行更新...尝试使用 UPDATE 代替

UPDATE upd_services t JOIN upd_services_changes c
ON t.srv_id = c.srv_id
SET t.srv_desc = COALESCE(c.srv_desc, t.srv_desc),
t.srv_list_price = COALESCE(c.srv_list_price, t.srv_list_price);

这里是SQLFiddle 演示

或者,如果 upd_services_changes 也包含新记录,并且您希望将它们与现有记录的更新一起插入到 upd_services 中,则

REPLACE INTO upd_services (srv_id, srv_desc, srv_list_price)
SELECT c.srv_id, COALESCE(c.srv_desc, t.srv_desc), COALESCE(c.srv_list_price, t.srv_list_price)
FROM upd_services_changes c LEFT JOIN upd_services t
ON c.srv_id = t.srv_id;

这里是SQLFiddle 演示

或者使用ON DUPLICATE KEY语法,恕我直言,这更合适

INSERT INTO upd_services (srv_id, srv_desc, srv_list_price)
SELECT c.srv_id, COALESCE(c.srv_desc, t.srv_desc), COALESCE(c.srv_list_price, t.srv_list_price)
FROM upd_services_changes c LEFT JOIN upd_services t
ON c.srv_id = t.srv_id
ON DUPLICATE KEY UPDATE srv_desc = VALUES(srv_desc),
srv_list_price = VALUES(srv_list_price);

这里是SQLFiddle 演示

关于mysql - 使用单个 Replace 语句更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20041214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com