2

我希望更新的实际字段根据条件进行更改。我遇到的现有示例只允许更改更新的值,而不是字段。这是我尝试过的:

UPDATE conversations 
CASE WHEN conv_author_id = $uid THEN SET conv_viewed_author = $d
ELSE SET conv_viewed_recipient = $d END 
WHERE conv_id = $id

但是,这给了我一个语法错误。

如何将更新的实际字段设置为有条件的?

4

2 回答 2

2

这应该可以解决问题:

UPDATE conversations
SET
  conv_viewed_author = IF(conv_author_id = $uid, $d, conv_viewed_author),
  conv_viewed_recipient = IF(conv_author_id = $uid, conv_viewed_recipient, $d)
WHERE
  conv_id = $id;
于 2013-11-11T16:42:56.750 回答
2

你总是可以做这样的事情:

UPDATE conversations
SET 
    conv_viewed_author = (
        CASE conv_author_id 
            WHEN $uid THEN $d 
            ELSE conv_viewed_author 
        END
    ),
    conv_viewed_recipient = (
        CASE conv_author_id 
            WHEN $uid THEN conv_viewed_recipient 
            ELSE $d 
        END
    )
WHERE conv_id = $id
于 2013-11-11T16:40:59.187 回答