2017-06-14 75 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