我想更新“location_costs”表下“animals”表基礎成本下的“location_cost”列。主鍵是連接兩個表的位置。我嘗試了以下代碼,但它給了我語法錯誤。
//UPDATE animals.*
SET animals.location_cost = location_costs.costs
FROM animals
LEFT JOIN location_costs
ON animals.location = location_costs.location;//
錯誤:“SET”處或附近的語法錯誤 SET Animal.location_cost = location_costs.costs
我附上了一張圖片,它給出了這里的表格和列的想法:
表格視圖
我無法破譯錯誤,如果有人可以幫助我使用此代碼,我將不勝感激。
謝謝你。
uj5u.com熱心網友回復:
如果您想更新只行中animals有一個匹配location的location_costs,然后使用這個語法:
UPDATE animals
SET location_cost = location_costs.costs
FROM location_costs
WHERE location_costs.location = animals.location;
如果要更新 的所有行animals(即使沒有匹配locationin的行location_costs也會更新為null),則使用相關子查詢:
UPDATE animals
SET location_cost = (
SELECT location_costs.costs
FROM location_costs
WHERE location_costs.location = animals.location
);
uj5u.com熱心網友回復:
如果animalid 是具有唯一值的列,請嘗試使用如下別名:
Update animals
Set location_cost = location_costs.costs
From animals As a Left Join location_costs On (a.location = location_costs.location)
Where animals.animalid = a.animalid;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/354201.html
標籤:sql PostgreSQL 加入 sql更新
上一篇:GoogleColab-無法再安裝GoogleDrive-瀏覽器彈出視窗(GoogleDriveforDesktop)而不是代碼輸出中的鏈接以進行授權
