我正在使用 SQLite,我有兩個表:
table1 table2
------------- --------------
id id
value condition
“id”列包含相同的資料。我需要:
UPDATE table1
SET table1.value = 'Some value'
WHERE table2.condition = 'Some condition"
我嘗試使用JOIN和FROM將表與“id”列鏈接,但這在 SQLite 中不起作用。請幫助語法。
uj5u.com熱心網友回復:
您需要更新中的相關子查詢,如下所示:
UPDATE table1
SET value = 'Some value'
WHERE EXISTS (SELECT 1 FROM table2 t2
WHERE t2.id = table1.id AND
t2.condition = 'Some condition');
uj5u.com熱心網友回復:
方法 1:考慮到您擁有的兩個表是“Table1”和“Table2”,更新 Table1 可以使用嵌套的 Select 陳述句完成,您將從 Table2 中選擇需要更新的資料。例如:
UPDATE Table1
SET StudentID =
(
SELECT RegNo
FROM Table2 t
WHERE StudentID = RegNo
);
您可以查看解決類似問題的此鏈接:https ://dba.stackexchange.com/questions/206317/update-values-in-one-table-from-data-in-another
方法2:您可以使用表連接。請參閱上面給出的相同鏈接。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435148.html
標籤:sqlite
