不能在UPDATE查询中使用子查询?

时间:2012-04-29 14:36:16

标签: php mysql sql

当我使用此查询时,MySQL会显示“HY000”错误:“您无法在FROM子句中为更新指定目标表'msg_pv'。”

问题似乎是查询和子查询的目标是同一个表......但这是我需要做的!

 <?php  $requete = $pdo->prepare('UPDATE msg_pv SET 
        lu=:lu
        WHERE id_ref_msg = :id_ref_msg AND date_message > ( SELECT MIN(date_message) FROM msg_pv WHERE id_ref_msg = :id_ref_msg AND lu="0" )');

注意:我已经在INNER JOIN的其他帖子中阅读了一些解决方案,但它适用于不同的表格。

1 个答案:

答案 0 :(得分:3)

试试这个

UPDATE
  msg_pv AS t1
CROSS JOIN (
  SELECT MIN(date_message) AS date_message FROM msg_pv
  WHERE id_ref_msg = :id_ref_msg AND lu="0"
) AS t2
SET
  t1.lu = :lu
WHERE
  t1.date_message > t2.date_message