2017-06-14 76 views
-1

SQL问题。我把system_id作为partent_id与适当的parent_id(这是表company_id)进行比较。所以我想这一点,但我得到几个错误:更新表格与来自同一表格的值

update justmarket.companies e, (Select DISTINCT company_id from justmarket.companies where parent_id = system_id) c 
set e.parent_id = c.company_id 
where e.company_id = c.company_id 

结束错误:

Error Static analysis:

6 errors were found during analysis.

An expression was expected. (near "(" at position 31) Unexpected token. (near "(" at position 31) A new statement was found, but no delimiter between it and the previous one. (near "Select" at position 32) Unexpected token. (near ")" at position 112) Unexpected token. (near "c" at position 114) A new statement was found, but no delimiter between it and the previous one. (near "set" at position 117) SQL query: Documentation

update justmarket.companies e, (Select DISTINCT company_id from justmarket.companies where parent_id = system_id) c set e.parent_id = c.company_id where e.company_id = c.company_id

MySQL said: Documentation

1205 - Lock wait timeout exceeded; try restarting transaction

表: COMPANY_ID COMPANY_NAME SYSTEM_ID PARENT_ID 1名1 55121 0 2名2 52211 55121 3 NAME3 55444 55121

我在做什么 company_id company_name system_id parent_id 1名1 55121 0 2名称2 52211 1 3 NAME3 55444 1

+2

样本数据和期望的结果真的有帮助。 –

+0

我认为第一个错误是PhpMyAdmin中的错误,它不理解多表'UPDATE'语法。 – Barmar

+0

查看https://stackoverflow.com/questions/35608945/mysql-replace-statement-incorrect-a-new-statement-was-found-but-no-delimiter获取该错误的另一个查询。 – Barmar

回答

0

尝试使用显式JOIN语法而不使用子查询。

UPDATE companies AS e 
JOIN companies AS c ON e.company_id = c.company_id 
SET e.parent_id = c.company_id 
WHERE c.parent_id = c.system_id