我正在尝试使用具有更改的表来更新表。插入内容是:
-- 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进行更新,我已经完成了。我也是 如果它是null,则不需要更改services表中的描述,如果它为null,则不更改price字段,但是我被卡住了。这就是我到目前为止所做的:
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);
答案 0 :(得分:1)
由于您的目标是 ...仅对现有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 演示
或使用{1更合适的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 演示