开发者

pass column in INSERT query

I want to update and insert to (in 1 query) my table. I've 1 new row and 2 old records. I use this query (topic):

INSERT INTO table (id, title, url) VALUES (1, "test", "http://"), (2, [it is an old record. I don't want to change its title], "https://"), (3, "helloworld", "mms://") ON DUPLICATE KEY UPDATE title = VALUES(title), u开发者_JAVA百科rl = VALUES(url)

I don't want to change the second record (id = 2) title column, but if I put NULL, DEFAULT or title after comma, title will change to NULL (also if i put nothing after comma, an error will occur)

What can I do? should I use 2 or more queries?

Thank you.


You can use something like:

INSERT INTO table (id, title, url) VALUES (1, "test", "http://"), (2, "", "https://"), (3, "helloworld", "mms://") ON DUPLICATE KEY UPDATE title = IF(VALUES(title)="",title,VALUES(title)), url = VALUES(url)


Not 100% clear on your goal but could you not nest a SELECT that would pull back the old title for in place of the text in square brackets?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜