[英]What is the correct SQL Syntax?
我正在努力解決Laravel Eloquent查詢中的SQL語法問題。 因此,我回到了根源,並試圖使查詢在簡單的SQL中工作。 而且我很沮喪。 我不是專家,但這讓我感到困惑。
我看過《 在SQLite中使用Join更新》一文 ,但我認為其中更多的是關於join的問題。
我正在SQLLite上對此進行測試,這肯定可以代表SQL嗎?
兩張表:說明和已知。 我想更新(最新)說明中的已知內容中的一列。 正確處理,剩下的我自己解決(我希望!)。
-- This works fine
Select instructions.rowid from instructions
where instructions.EngagementTitle not null
-- This doesn't
UPDATE knowns
SET EngagementTitle = instructions.EngagementTitle
WHERE id IN (
SELECT knowns.id
FROM knowns
INNER JOIN instructions
ON knowns.reference = instructions.reference
)
錯誤信息
no such column: instructions.EngagementTitle:
UPDATE knowns
SET EngagementTitle = instructions.EngagementTitle
WHERE id IN (
SELECT knowns.id
FROM knowns
LEFT JOIN instructions
ON knowns.reference = instructions.reference
)
兩個表都具有列-三重選中。
`EngagementTitle` varchar NOT NULL
我想念什么?
UPDATE knowns
SET EngagementTitle = instructions.EngagementTitle
...
該instructions
表不“知”在這里,只有knowns
。 表表達式的范圍僅是“向下”。 您可以在子查詢的超級查詢中使用引入的表,但反之則不能。
嘗試使用子選擇:
UPDATE knowns
SET engagementtitle = (SELECT instructions.engagementtitle
FROM instructions
WHERE instructions.reference = knowns.reference)
WHERE EXISTS (SELECT *
FROM instructions
WHERE instructions.reference = knowns.reference);
我還用EXISTS
替換了您的WHERE
。 我想這就是您實際想要的方式。 它只會從更新行knowns
如果一個條目中存在instructions
進行。 在子查詢中使用LEFT JOIN
到IN
,您剛剛更新了所有行,因為LEFT JOIN
包括左表中的所有行,因此,所有knowns
ID都在結果中(您可能想要一個INNER JOIN
,那會起作用的。)。
但要注意,必須有不超過一個條目instructions
一行中knowns
這個工作。 我默默地假設情況就是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.