CASE(MySQL)内部更新

时间:2009-08-15 07:15:42

标签: mysql

我有两个问题 第一:

SELECT
    players.username AS username,
    tmp_player_operations.id AS tmp_id, 
    tmp_player_operations.operation_type AS operation_type, 
    tmp_player_operations.veryfication_code AS veryfication_code, 
    tmp_player_operations.expiration_date AS expiration_date,
    tmp_player_operations.used AS used
FROM
    players LEFT OUTER JOIN tmp_player_operations 
    ON players.id = tmp_player_operations.player_id
WHERE
tmp_player_operations.veryfication_code = '3c3c4375086fbcbc1cef938a0bfabbb9'

第二

UPDATE 
    players LEFT JOIN tmp_player_operations 
    ON players.id = tmp_player_operations.player_id
SET 
    tmp_player_operations.used = 1,
    players.active = 1
WHERE
    tmp_player_operations.id = 8

现在我想在select中进行此更新,我的意思是:

(CASE
   WHEN
      tmp_player_operations.used = 0
      THEN
          UPDATE....
          AND 'updated'
   ELSE
      'not updated'
END) AS something

是这样的吗?

2 个答案:

答案 0 :(得分:1)

不,这是不可能的。查询可以是SELECTUPDATE,但不能同时为。{/ p>

我想,您可以使用存储过程进行一次数据库调用以执行更新和选择,但这仍然是两个查询(但该应用程序只会看到一个,即存储的proc调用) 。 Chapter 18 of the MySQL manual包含有关存储过程的详细信息。

也许你应该稍微退一步,问我们如何实现你的实际目标是什么?

答案 1 :(得分:0)

使用值1更新其他字段。然后再运行一个查询并选择"Updated",其中该字段等于一。