Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 2不能在FROM子句SQL2.sql 7 15中为更新指定目标表“p”_Mysql_Updates - Fatal编程技术网

Mysql 2不能在FROM子句SQL2.sql 7 15中为更新指定目标表“p”

Mysql 2不能在FROM子句SQL2.sql 7 15中为更新指定目标表“p”,mysql,updates,Mysql,Updates,这是我的疑问: UPDATE pupils p SET p.rollIdentity = NULL WHERE p.id IN (SELECT pup469.id FROM pupils pup469 inner JOIN pupils pup470 ON pup470.rollIdentity = pup469.rollIdentity where pup469.school_id = 469 and pup469.year = 10 AND pup470.school_id = 470 AN

这是我的疑问:

UPDATE pupils p
SET p.rollIdentity = NULL
WHERE p.id IN (SELECT pup469.id FROM pupils pup469
inner JOIN pupils pup470 ON pup470.rollIdentity = pup469.rollIdentity
where pup469.school_id = 469 and pup469.year = 10
AND pup470.school_id = 470 AND pup470.year = 3)
所以基本上,我只需要更新在另一所学校有相同身份的学生。
我读到我应该用exist来代替,但我不完全理解这一点,有人能进一步解释一下吗?如果您将子查询埋得更深一点,谢谢

drop table if exists t;
create table t(id int,school_id int, rollIdentity int, yr int);
insert into t values
(1,470, 10,3),
(2, 470, null,2),
(3, 469, 10,10),
(4,34,10,4);

UPDATE t p
SET p.rollIdentity = NULL
WHERE p.id IN 
(
select id from
(
SELECT pup469.id
  FROM t pup469
inner JOIN t pup470 ON pup470.rollIdentity = pup469.rollIdentity
where (pup469.school_id = 469 and pup469.yr = 10)
AND   (pup470.school_id = 470 AND pup470.yr = 3)
) s
);

select * from t;
你明白了吗

+------+-----------+--------------+------+
| id   | school_id | rollIdentity | yr   |
+------+-----------+--------------+------+
|    1 |       470 |           10 |    3 |
|    2 |       470 |         NULL |    2 |
|    3 |       469 |         NULL |   10 |
|    4 |        34 |           10 |    4 |
+------+-----------+--------------+------+
4 rows in set (0.00 sec)

但是为什么您不想更改id 1?

测试:从存在的学生p中选择p.id从存在的学生p中选择*从学生pup469内部加入学生pup470在pup470上。rollIdentity=pup469。rollIdentity其中pup469.school_id=469和pup469.year=10和pup470.school_id=470和pup470.year=3返回不同数量的行,因此,这不是相同的示例数据。。我相信您的查询可以变得更简单。id school_id rollIdentity={1470,10;2470,null;3469,10;4,34,10}在前面的示例中,3469,10应该更新为3469,null only在您的示例中是哪一年?