$rs = mysql_query("
SELECT a.id
FROM a
JOIN b ON b.id=a.id
WHERE
b.p1=1 AND
a.p1=1");
while($r = mysql_fetch_assoc($rs))
$ids[] = $r['id'];
$rs = mysql_query("
UPDATE a
SET p2=2
WHERE id IN (".implode(",",$ids).")");
如何在一个查询中执行此操作?
答案 0 :(得分:20)
UPDATE a
JOIN b ON a.id=b.id AND a.p1 = 1 AND b.p1 = 1
SET a.p2 = 2
答案 1 :(得分:5)
您可以使用INNER JOIN
执行此操作,其效果优于IN
子句:
UPDATE a
INNER JOIN b
ON b.id=a.id
AND b.p1 = 1
AND a.p1 = 1
SET p2 = 2;
答案 2 :(得分:3)
UPDATE a
SET p2=2
WHERE id IN (SELECT a.id
FROM a
JOIN b ON b.id=a.id
WHERE
b.p1=1 AND
a.p1=1)