Database 使用另一个表中的非空值更新sqlite表
我有两个sqlite表,它们都具有相同的列布局。我希望合并这两个表(或更新表1),如果它们不为null,则从表2中获取值,否则从表1中获取值。有没有更好的方法来做到这一点,即使用多个Database 使用另一个表中的非空值更新sqlite表,database,sqlite,join,select,left-join,Database,Sqlite,Join,Select,Left Join,我有两个sqlite表,它们都具有相同的列布局。我希望合并这两个表(或更新表1),如果它们不为null,则从表2中获取值,否则从表1中获取值。有没有更好的方法来做到这一点,即使用多个case子句进行更新(类似于中的方法)?这些表有大量的列,这使得这样的语句相当长 |-------+-------+--------+----+--------| | id | col1 | col2 | .. | col100 | |-------+-------+--------+----+-----
case
子句进行更新
(类似于中的方法)?这些表有大量的列,这使得这样的语句相当长
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | null | green | | null |
| 1b7da | null | null | | GA |
| 9896f | plum | null | | null |
|-------+-------+--------+----+--------|
表1
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | apple | red | | WA |
| d27d7 | pear | green | | VA |
| 568ba | lemon | yellow | | CA |
| 9896f | grape | purple | | CA |
| 1b7da | peach | pink | | CA |
|-------+-------+--------+----+--------|
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | null | green | | null |
| 1b7da | null | null | | GA |
| 9896f | plum | null | | null |
|-------+-------+--------+----+--------|
表2
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | null | green | | null |
| 1b7da | null | null | | GA |
| 9896f | plum | null | | null |
|-------+-------+--------+----+--------|
期望结果
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | null | green | | null |
| 1b7da | null | null | | GA |
| 9896f | plum | null | | null |
|-------+-------+--------+----+--------|
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | apple | green | | WA |
| d27d7 | pear | green | | VA |
| 568ba | lemon | yellow | | CA |
| 9896f | plum | purple | | CA |
| 1b7da | peach | pink | | GA |
|-------+-------+--------+----+--------|
您可以使用
左联接带表2
,并使用coalesce()
对表2
中的非null
值进行优先级排序:
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | null | green | | null |
| 1b7da | null | null | | GA |
| 9896f | plum | null | | null |
|-------+-------+--------+----+--------|
您可以使用更新表1
:
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | null | green | | null |
| 1b7da | null | null | | GA |
| 9896f | plum | null | | null |
|-------+-------+--------+----+--------|
update Table1
set (col1, col2, col100) = (
select
coalesce(t2.col1, Table1.col1),
coalesce(t2.col2, Table1.col2),
................................
coalesce(t2.col100, Table1.col100)
from Table2 t2
where t2.id = Table1.id
)
where exists (select 1 from Table2 t2 where t2.id = Table1.id);
请参阅。
结果:
|-------+-------+--------+----+--------|
| id | col1 | col2 | .. | col100 |
|-------+-------+--------+----+--------|
| 2346a | null | green | | null |
| 1b7da | null | null | | GA |
| 9896f | plum | null | | null |
|-------+-------+--------+----+--------|
这回答了你的问题吗?由于表具有相同的列名,是否有办法绕过指定所有100条合并语句?或者是一种让sqlite从表模式生成它们的方法?