1

I have a table like this:

id | userid | commentid | value
-------------------------------

Each user is permitted to vote a comment once. The value can be between -1 and 1. Is there a easy way for a table model to achieve an change of this vote in one single query, without checking EXISTS() first? I already thought about a hash-column like this

MD5(CONCAT(userid, commentid))

but is there any better solution for this?

4

1 回答 1

3

Just use an unique key on userid + commentid and you can use ON DUPLICATE KEY UPDATE.

INSERT INTO `yourTable` (
  `userid`,
  `commentid`,
  `value`
) VALUES (
  x,
  y,
  z
) ON DUPLICATE KEY UPDATE
  `value` = z
于 2013-04-27T19:45:17.953 回答