gpt4 book ai didi

sqlite - SQLite-根据另一个表的列中的值更新列

转载 作者:行者123 更新时间:2023-12-03 18:45:20 27 4
gpt4 key购买 nike

我在以下情况下尝试将Record1的ID更新为Record2的ID:


两个表中的名称相同,并且
在Record2中权重更大。


记录1

| ID | Weight | Name |
|----|--------|------|
| 1 | 10 | a |
| 2 | 10 | b |
| 3 | 10 | c |


记录2

| ID | Weight | Name |
|----|--------|------|
| 4 | 20 | a |
| 5 | 20 | b |
| 6 | 20 | c |


我已经尝试了以下SQLite查询:

update record1
set id =
(select record2.id
from record2,record1
where record1.name=record2.name
and record1.weight<record2.weight)


使用上述查询,所有记录的Record1的ID更新为4。

最佳答案

编写SELECT ...record1引入了record1表的新实例,该实例隐藏了外部实例。

为了能够引用外部查询中的当前行,只需从FROM子句中删除table1

UPDATE record1
SET id = (SELECT record2.id
FROM record2
WHERE record1.name = record2.name
AND record1.weight < record2.weight);

关于sqlite - SQLite-根据另一个表的列中的值更新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38380389/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com