开发者

If row dont exist, insert instead of update

UPDATE userpostratings SET rating = $rating WHERE postID = 8

If a there isnt a row in userpostratings with postID = 8 i wanna INSERT instead. Could this be done with s开发者_Go百科ql or should I do it in php

Thanks

Tomek


see here: How do I update if exists, insert if not (AKA "upsert" or "merge") in MySQL?


If you have a UNIQUE index on postID, then you can do something like:

INSERT INTO userpostratings (rating, postID) VALUES ($rating, 8)
    ON DUPLICATE KEY UPDATE rating = $rating;


here you go

http://nl2.php.net/manual/en/function.mysql-affected-rows.php

> mysql_query("UPDATE mytable SET used=1
> WHERE id < 10"); printf ("Updated
> records: %d\n",
> mysql_affected_rows());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜