2013-09-01 63 views
4

这个MySQL错误我有这样的表。为什么我得到了更新SQL

CREATE TABLE `chart` (
     `id` int(10) NOT NULL AUTO_INCREMENT, 
     `item` char(6) NOT NULL DEFAULT '', 
     `IsLeaf` char(1) NULL DEFAULT 'Y', 
     `ParentId` int(10) NOT NULL DEFAULT 0, 
     PRIMARY KEY (`id`) 
    ) 

其中包含的parentId另一行的ID,这是该行 的父母一样

----------------------------------------------------------------- 
| Id | item | IsLeaf  | ParentId 
----------------------------------------------------------------- 
| 1 | Test1 | D   | 
----------------------------------------------------------------- 
| 2 | Test3 | D   | 
----------------------------------------------------------------- 
| 3 | Test4 | D   | 1 
----------------------------------------------------------------- 
| 4 | Test5 | D   | 1 
----------------------------------------------------------------- 

我想更新那些具有至少一个子行的行。我想这样的

UPDATE chart AS c1 SET c1.IsLeaf='Y' JOIN chart c2 ON c2.ParentId=c1.id; 

,并得到这个错误

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN chart c2 ON c2.ParentId=c1.id' at line 1 

回答

3

你必须使用Set关键字离开后JOIN和必须检查空

UPDATE chart AS c1 
LEFT JOIN chart c2 ON c2.ParentId=c1.id SET c1.IsLeaf='Y' 
WHERE c2.id is null; 
+0

感谢您的解释.... – SarwarCSE

2

尝试使用LEFT JOIN是这样的: -

UPDATE chart AS c1 
LEFT JOIN chart c2 ON c2.ParentId=c1.id 
SET c1.IsLeaf='Y' WHERE c2.id is null; 
+0

由于它的工作原理... – SarwarCSE

+0

我已经做到了这一点.. – SarwarCSE