将SELECT查询放在UPDATE查询的WHERE子句中时出现错误。
我的查询是这样的:
UPDATE `subschedulesseats` m SET m.studentid='1' WHERE m.`seatid`= ( SELECT h.`seatid` FROM `subschedulesseats` h WHERE h.`sessiontime`='02:30~04:00' ORDER BY h.`seatid` ASC LIMIT 2,1 )
AND错误将显示如下:
“无法在FROM子句中指定目标表’m’进行更新”
实际上,您可以通过将其包装在子查询中来更新它(从而为结果创建临时表)
UPDATE `subschedulesseats` m SET m.studentid='1' WHERE m.`seatid`= ( SELECT seatID FROM ( SELECT h.`seatid` FROM `subschedulesseats` h WHERE h.`sessiontime`='02:30~04:00' ORDER BY h.`seatid` ASC LIMIT 2,1 ) s )
或使用 JOIN
UPDATE `subschedulesseats` m INNER JOIN ( SELECT seatID FROM ( SELECT h.`seatid` FROM `subschedulesseats` h WHERE h.`sessiontime`='02:30~04:00' ORDER BY h.`seatid` ASC LIMIT 2,1 ) s ) t ON m.seatID = t.seatID SET m.studentid = '1'