2012-04-26 53 views
0

我有一個查詢的MySQL的if..else與子查詢

SELECT IF((select COUNT(*) from produkty where name='ASROCK 760GM-GS3' and id_nokaut='7507601876693181035' and found ='0')=0, (insert into produkty (
     id_nokaut, 
     name, 
     shop_count, 
     offer_count, 
     price_min, 
     price_max, 
     price_avg, 
     url, 
     opis, 
     socket, 
     typ_pamieci, 
     co, 
     image_mini, 
     image_medium, 
     image_large, 
     rate, 
     thumbnail, 
     image, 
     found) 
     values(
     '7507601876693181035', 
     'ASROCK 760GM-GS3', 
     '4', 
     '4', 
     '171,31', 
     '195,00', 
     '179,88', 
     'http://www.nokaut.pl/plyty-glowne/asrock-760gm-gs3.html', 
     'ASRock 760GM-GS3... ', 
     'false', 
     'false', 
     'plyta', 
     'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc90x90/asrock-760gm-gs3.jpg', 
     'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc130x130/asrock-760gm-gs3.jpg', 
     'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc500x500/asrock-760gm-gs3.jpg', 
     '0.00', 
     'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc90x90/asrock-760gm-gs3.jpg', 
     'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc130x130/asrock-760gm-gs3.jpg', 
     '1')), (update produkty set id_nokaut = '7507601876693181035', shop_count = '4', offer_count = '4', price_min = '171,31', price_max = '195,00', price_avg = '179,88', url = 'http://www.nokaut.pl/plyty-glowne/asrock-760gm-gs3.html', opis = 'ASRock 760GM-GS3... ', socket = 'false', typ_pamieci = 'false', co = 'plyta', image_mini = 'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc90x90/asrock-760gm-gs3.jpg', image_medium = 'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc130x130/asrock-760gm-gs3.jpg', image_large = 'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc500x500/asrock-760gm-gs3.jpg', rate = '0.00', thumbnail = 'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc90x90/asrock-760gm-gs3.jpg', image = 'http://nokautimg1.pl/p-da-99-da99e9e7b3eaebef049d1234fc5c15dc130x130/asrock-760gm-gs3.jpg', found = '1' where name = 'ASROCK 760GM-GS3')); 

我想在MySQL中執行它,但它給了MI錯誤:

ERROR 1064 (42000) at line 4: You have an error in your SQL syntax; check the ma nual that corresponds to your MySQL server version for the right syntax to use n ear 'into produkty ( id_nokaut, name, shop_cou' at line 1

我想不出什麼是錯的,任何人都可以幫忙嗎?

感謝

+2

它看起來像你打算這樣做非常困難的辦法['REPLACE'(http://dev.mysql.com/doc/refman/5.0/en/replace。 html)或['INSERT ... ON DUPLICATE KEY UPDATE'](http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html)。然而,如果你必須**使用IF,那麼你需要MySQL的['IF'語句](http://dev.mysql.com/doc/refman/5.5/en/if-statement.html) - 它不構成'SELECT'命令的一部分 - 而不是['IF'函數](http://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html#function_if)你目前正在使用(我知道,這很混亂!)。 – eggyal 2012-04-26 17:00:26

回答

3
INSERT 
INTO produkty (id_nokaut, name, ...) 
VALUES ('7507601876693181035', 'ASROCK 760GM-GS3', ...) 
ON DUPLICATE KEY 
UPDATE 
     shop_count = VALUES(shop_count), 
     offer_count = VALUES(offer_count), 
     ... 
+0

OP可能需要一種方法來更新多行並在沒有找到時插入,這種方法僅對單行有效。 – piotrm 2012-04-26 17:09:43

+0

@piotrm:'id'通常意味着一個'PRIMARY KEY'。 – Quassnoi 2012-04-26 17:13:50

+0

沒有用於限制行更新的id。 – piotrm 2012-04-26 17:19:59