2016-09-06 65 views
0

我想基於另一個表中的一些匹配的列在一個表中填充表的ID的,這裏是我的查詢:用SELECT MySQL的INSERT根據匹配列

INSERT INTO adverts (publication_id) VALUES (SELECT publications.id FROM publications WHERE publications.name LIKE CONCAT ('%', (adverts.publication), '%')); 

1064 - 你必須你的SQL語法錯誤;檢查手冊中 對應於你的MySQL服務器版本正確的語法使用 附近「SELECT publications.id FROM出版物WHERE publications.name LIKE CONCAT(」%」在1號線

不知道在哪裏我要去錯在這裏,任何人可以幫助

下面是我的2臺結構:

廣告表: enter image description here

公佈ications表: enter image description here

編輯 下面你可以找到兩個表中的一些樣本數據,最好是什麼,我想實現的是插入來自publicationsid值到publications_id的基礎上,adverts表是否在advert.publication - publications.name列中有匹配項。

廣告的示例數據: enter image description here

出版物樣本數據:樣本數據的 enter image description here

文本格式:

廣告:

"56d4085e-654c-47e1-86f0-1e7f5850bebe",,"Saturday 27th February 2016","The Daily Telegraph","188833f6-7405-11e6-96f3-0800274f2cef","2016-09-06","pdf/1/a/1a5a83.pdf","0","2016-02-29 08:59:10","2016-09-06 08:31:50" 
"56d71a59-60cc-4a9b-9170-0eb15850bebe",,"Sunday 28th February 2016","The Scottish Sunday Mail",,"2016-02-28","jpg/a/8/a8d73b.jpg","0","2016-03-02 16:52:41","2016-03-02 16:53:13" 
"56daa0f6-a2c8-4346-ac05-6f825850bebe",,"Saturday 5th March 2016","The Daily Telegraph",,"2016-03-05","pdf/9/e/9eda94.pdf","0","2016-03-05 09:03:50","2016-03-05 09:04:59" 
"56daa30b-f8c4-40aa-8197-6f555850bebe",,"Friday 12th February 2016","Daily Star",,"2016-02-12","pdf/9/2/921346.pdf","0","2016-03-05 09:12:43","2016-03-05 09:13:26" 
"56daa37c-a46c-4446-9fa5-6f825850bebe",,"Sunday 14th February 2016","Sunday Express",,"2016-02-14","pdf/3/b/3b8fc2.pdf","0","2016-03-05 09:14:36","2016-03-05 09:14:36" 
"56daa3ee-441c-4bb0-b9ff-6f555850bebe",,"Saturday 27th February 2016","Daily Express",,"2016-02-27","pdf/e/b/ebb99b.pdf","0","2016-03-05 09:16:30","2016-03-05 09:16:30" 
"56daa44f-4124-41bd-bb34-6fb95850bebe",,"Sunday 28th February 2016","Daily Star Sunday ",,"2016-02-28","pdf/1/e/1e412d.pdf","0","2016-03-05 09:18:07","2016-03-05 09:18:07" 

出版物:

"000f371a-7405-11e6-96f3-0800274f2cef","The Daily Telegraph","png/6/9/69e409.png","2016-09-06 00:00:00","2016-09-06 07:43:31" 
"10895d76-7405-11e6-96f3-0800274f2cef","The Scottish Sunday Mail","jpg/4/1/41b6e6.jpg","2016-09-06 00:00:00","2016-09-06 07:44:02" 
"188833f6-7405-11e6-96f3-0800274f2cef","Daily Star","png/d/c/dcb8dd.png","2016-09-06 00:00:00","2016-09-06 07:44:22" 
"212778a5-7405-11e6-96f3-0800274f2cef","Sunday Express","gif/5/2/529a66.gif","2016-09-06 00:00:00","2016-09-06 07:45:57" 
"2fa48a98-7405-11e6-96f3-0800274f2cef","Daily Express","gif/6/c/6c5be4.gif","2016-09-06 00:00:00","2016-09-06 07:46:19" 
"37d0b036-7405-11e6-96f3-0800274f2cef","Daily Star Sunday","png/c/e/ce7c42.png","2016-09-06 00:00:00","2016-09-06 07:46:44" 
"3f78db05-7405-11e6-96f3-0800274f2cef","Scottish Daily Mail","jpg/5/c/5caf2f.jpg","2016-09-06 00:00:00","2016-09-06 07:47:10" 

回答

0

你必須使用一個UPDATE不是INSERT聲明:

UPDATE Adverts a 
JOIN Publications p ON a.publication LIKE CONCAT('%', (p.name), '%') 
SET a.publication_id = p.id 

Demo here

+0

只是嘗試這樣做,現在我得到'#1054 - 'where子句'中的未知列'adverts.publication' – user3574492

+0

@ user3574492 Ooops,我的壞。你不能在語句的SELECT部分​​使用'adverts'表。你究竟在努力實現什麼? –

+0

我有一堆出版物可以有很多廣告(廣告屬於出版物)。我試圖將出版物的出版物ID插入廣告所屬的廣告表中。如果這是有道理的? – user3574492

0

正確的語法是:

INSERT INTO adverts (publication_id) 
    SELECT publications.id 
    FROM publications join adverts on <condition> 
    WHERE publications.name LIKE CONCAT ('%', (adverts.publication), '%'));