不能指定目标表以便在FROM子句中更新我有一个简单的MySQL表:CREATE TABLE IF NOT EXISTS `pers` (
`persID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(35) NOT NULL,
`gehalt` int(11) NOT NULL,
`chefID` int(11) DEFAULT NULL,
PRIMARY KEY (`persID`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;INSERT INTO `pers`
(`persID`, `name`, `gehalt`, `chefID`) VALUES(1, 'blb', 1000, 3),(2, 'as', 1000, 3),(3, 'chef', 1040, NULL);我试图运行以下更新,但只得到了错误1093:UPDATE pers P
SET P.gehalt = P.gehalt * 1.05 WHERE (P.chefID IS NOT NULL OR gehalt < (SELECT (
SELECT MAX(gehalt * 1.05)
FROM pers MA
WHERE MA.chefID = MA.chefID)
AS _pers))我搜索了错误,并在下面的MySQL页面中找到http:/dev.mysql.com/doc/reflman/5.1/en/subquery-strations.html但这帮不了我。如何纠正SQL查询?
3 回答
慕妹3146593
TA贡献1820条经验 获得超9个赞
UPDATE myTableSET myTable.A =( SELECT B FROM myTable INNER JOIN ...)
UPDATE
/INSERT
/DELETE
myTable
(SELECT * FROM myTable)
UPDATE myTableSET myTable.A =( SELECT B FROM (SELECT * FROM myTable) AS something INNER JOIN ...)
你不会想 SELECT * FROM table
在现实生活中的子查询中,我只想保持示例的简单性。实际上,您只应该在最内部的查询中选择所需的列,并添加一个好的 WHERE
子句来限制结果。
德玛西亚99
TA贡献1770条经验 获得超3个赞
CREATE TABLE test2 ASSELECT PersId FROM pers pWHERE ( chefID IS NOT NULL OR gehalt < ( SELECT MAX ( gehalt * 1.05 ) FROM pers MA WHERE MA.chefID = p.chefID ))
...
UPDATE pers PSET P.gehalt = P.gehalt * 1.05WHERE PersIdIN ( SELECT PersId FROM test2)DROP TABLE test2;
UPDATE Pers P, ( SELECT PersId FROM pers p WHERE ( chefID IS NOT NULL OR gehalt < ( SELECT MAX ( gehalt * 1.05 ) FROM pers MA WHERE MA.chefID = p.chefID ) )) tSET P.gehalt = P.gehalt * 1.05WHERE p.PersId = t.PersId
缥缈止盈
TA贡献2041条经验 获得超4个赞
UPDATE TABLE_A AS A INNER JOIN TABLE_A AS B ON A.field1 = B.field1 SET field2 = ?
添加回答
举报
0/150
提交
取消