带有SELECT的MySQL UPDATE(标记"项目"如读)

时间:2013-01-10 14:07:17

标签: mysql sql select join sql-update

我有以下查询从“ac_pms”表中选择个人消息(PM)。其他信息来自其他表 - “ac_accounts”使用LEFT JOIN。 “pm_read”表中有“ac_pms”列,用于定义是否读取PM。我需要在选择PM时将该字段(pm_read)设置为“1”。

SELECT p.*, a.seller_id, a.winner_id 
FROM `ac_pms` AS p 
LEFT JOIN `ac_accounts` AS a ON p.pm_for_lot = a.id 
WHERE (p.pm_from=[user_id] OR p.pm_to=[user_id]) 
  AND p.pm_for_lot=[account_id] 
ORDER BY p.pm_date DESC;

我无法想象将UPDATE表达式插入SET pm_read = 1的位置。

1 个答案:

答案 0 :(得分:1)

在同一个SQL语句中,SELECT不能UPDATE

但是,UPDATE JOIN可以这样:

UPDATE ac_pms AS p 
LEFT JOIN ac_accounts AS a ON p.pm_for_lot = a.id 
SET p.pm_read = 1
WHERE (p.pm_from=[user_id] OR p.pm_to=[user_id]) 
  AND p.pm_for_lot = [account_id];

然后你可以在那之后再制作SELECT