-1
表:的MySQL相當於執行UPDATE SET FROM /合併成
CREATE TABLE `vendor_contacts` (
`vendor_id` int(11) NOT NULL,
`last_name` varchar(50) NOT NULL,
`first_name` varchar(50) NOT NULL,
`name_initials` varchar(45),
PRIMARY KEY (`vendor_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
用下面的插入語句:
INSERT INTO `vendor_contacts`
VALUES (5,'Davison','Michelle',''),
(12,'Mayteh','Kendall',''),
(17,'Onandonga','Bruce',''),
(44,'Antavius','Anthony',''),
(76,'Bradlee','Danny',''),
(94,'Suscipe','Reynaldo',''),
(101,'O\'Sullivan','Geraldine',''),
(123,'Bucket','Charles','');
我試圖將列分配,name_initials用下面的命令,從PostgreSQL派生[更新>集>從]:
UPDATE vendor_contacts
SET name_initials = t.initials
FROM (
SELECT vendor_id, last_name, first_name,
CONCAT(LEFT(first_name, 1), LEFT(last_name, 1)) as initials
FROM vendor_contacts;
) t
WHERE vendor_id = t.vendor_id;
由於沒有工作,我試了甲骨文SQL合併到:
MERGE INTO vendor_contacts
USING (
SELECT vendor_id, last_name, first_name,
CONCAT(LEFT(first_name, 1), LEFT(last_name, 1)) as initials
FROM vendor_contacts;
) t
ON (vendor_id = t.vendor_id)
WHEN matched THEN
UPDATE SET name_initials = t.initials;
MERGE沒有工作。
我嘗試了子查詢,沒有工作,以及:
UPDATE vendor_contacts vc
SET name_initials = (SELECT CONCAT(LEFT(first_name, 1), LEFT(last_name, 1))
FROM vendor_contacts vcsq
WHERE vcsq.vendor_id = vc.vendor_id);
簡單的'UPDATE'有什麼問題?我沒有看到你插入任何東西。 – shmosel
有*問題*嗎?將觀察到的行爲描述爲「*不起作用」是不精確的,並且在診斷問題時實際上毫無用處。如果對行爲的描述更加精確,例如MySQL返回錯誤1093「無法在FROM子句中引用目標表'vendor_contacts'進行更新」......這是一個更具體的問題,並帶有一些特定的解決方法。而且由於MySQL不支持'MERGE'語句,我們可能會推測MySQL返回了錯誤1046「SQL語法錯誤」。爲什麼rigmarole?爲什麼不只是一個單一的表更新? – spencer7593