开发者

Can IF be used to start a MySQL query?

I have a query that looks like this:

mysql_query("IF EXISTS(SELECT * FROM predict WHERE uid=$i AND bid=$j)
            THEN UPDATE predict SET predict_tfidf=$predict_tfidf WHERE uid=$i AND bid=$j
            ELSE INSERT INTO predict (uid开发者_开发知识库, bid, predict_tfidf) VALUES('$i','$j','$predict_tfidf')
            END IF")or die(mysql_error());

But it dies and mysql tells me to check the syntax near IF EXISTS(....)

Can we not use an IF statement to start a mysql query?

[EDIT] Thanks for the ON DUPLICATE KEY UPDATE tip, but what if I don't have any primary or unique keys in the table?

Thank you in advance.


No you can't do that (unless in stored procedure, and even then the syntax would be different)

Check INSERT INTO ... ON DUPLICATE KEY UPDATE syntax

http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜