Mysql 使用联接更新SQL查询
下面是我的SELECT语句,它列出了我试图更改的内容Mysql 使用联接更新SQL查询,mysql,sql,join,set,left-join,Mysql,Sql,Join,Set,Left Join,下面是我的SELECT语句,它列出了我试图更改的内容 SELECT t1.`qid`, t1.`gid` as 'incorrect gid', t1.`question` as 'subquestion', t1.`parent_qid`, t2.`qid`, t2.`gid` as 'correct gid', t2.`question` FROM `questions` as t1 LEFT JOIN `questions` as t2 ON t1.`parent_qid` = t
SELECT
t1.`qid`,
t1.`gid` as 'incorrect gid',
t1.`question` as 'subquestion',
t1.`parent_qid`,
t2.`qid`,
t2.`gid` as 'correct gid',
t2.`question`
FROM `questions` as t1
LEFT JOIN `questions` as t2 ON t1.`parent_qid` = t2.`qid`
WHERE t1.`sid` = '33844' AND t1.`gid` NOT IN ('1306','1317','1319','1320','1321','1322','1323','1324','1325','1326','1327','1328','1329','1330','1331','1332','1333','1334','1335''1334','1335','1336', '1337','1338','1339','1340')
我想使用UPDATE语句将所有“不正确的gid”替换为“正确的gid”
这是我尝试过的更新语句,但无法开始工作
UPDATE `questions` as t1
SET `gid` = t2.`gid`
FROM
`questions` as t1
LEFT JOIN
`questions` as t2 ON t1.`parent_qid` = t2.`qid`
WHERE t1.`sid` = '33844' AND t1.`gid` NOT IN ('1306','1317','1319','1320','1321','1322','1323','1324','1325','1326','1327','1328','1329','1330','1331','1332','1333','1334','1335''1334','1335','1336', '1337','1338','1339','1340')
基本上,所有子行的gid(组id)都不正确,需要修复。但是引用父级的所有行(因为父级和子级在同一个表中)都将具有正确的组id
因此,我必须将父groupid连接到每个子行。MySQL中的正确语法:
UPDATE `questions` as t1 LEFT JOIN
`questions` as t2 ON t1.`parent_qid` = t2.`qid`
SET t1.`gid` = t2.`gid`
WHERE t1.`sid` = '33844' AND
t1.`gid` NOT IN ('1306','1317','1319','1320','1321','1322','1323','1324','1325','1326','1327','1328','1329','1330','1331','1332','1333','1334','1335''1334','1335','1336', '1337','1338','1339','1340')
显然你不应该用这个盒子说“谢谢”,但我还是要用它!谢谢成功了。