如何使用 SQLite 中另一个 table 的 WHERE 条件更新 table
How can I UPDATE table using condition WHERE from another table in SQLite
我正在使用 SQLite,我有两个表:
table1 table2
------------- --------------
id id
value condition
“id”列包含相同的数据。我需要:
UPDATE table1
SET table1.value = 'Some value'
WHERE table2.condition = 'Some condition"
我尝试使用 JOIN 和 FROM 来链接带有“id”列的表,但这在 SQLite 中不起作用。请帮助语法。
您需要在更新中使用相关子查询,如下所示:
UPDATE table1
SET value = 'Some value'
WHERE EXISTS (SELECT 1 FROM table2 t2
WHERE t2.id = table1.id AND
t2.condition = 'Some condition');
方法 1:考虑到您拥有的两个 table 是“Table1”和“Table2”,更新 Table1 可以使用嵌套的 Select 语句,您将在其中选择需要从 Table2 更新的数据。例如:
UPDATE Table1
SET StudentID =
(
SELECT RegNo
FROM Table2 t
WHERE StudentID = RegNo
);
你可以看看这个 link,它解决了一个类似的问题:https://dba.stackexchange.com/questions/206317/update-values-in-one-table-from-data-in-another
方法 2:您可以使用 table 联接。参考上面给出的相同 link。
我正在使用 SQLite,我有两个表:
table1 table2
------------- --------------
id id
value condition
“id”列包含相同的数据。我需要:
UPDATE table1
SET table1.value = 'Some value'
WHERE table2.condition = 'Some condition"
我尝试使用 JOIN 和 FROM 来链接带有“id”列的表,但这在 SQLite 中不起作用。请帮助语法。
您需要在更新中使用相关子查询,如下所示:
UPDATE table1
SET value = 'Some value'
WHERE EXISTS (SELECT 1 FROM table2 t2
WHERE t2.id = table1.id AND
t2.condition = 'Some condition');
方法 1:考虑到您拥有的两个 table 是“Table1”和“Table2”,更新 Table1 可以使用嵌套的 Select 语句,您将在其中选择需要从 Table2 更新的数据。例如:
UPDATE Table1
SET StudentID =
(
SELECT RegNo
FROM Table2 t
WHERE StudentID = RegNo
);
你可以看看这个 link,它解决了一个类似的问题:https://dba.stackexchange.com/questions/206317/update-values-in-one-table-from-data-in-another
方法 2:您可以使用 table 联接。参考上面给出的相同 link。