Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 使用另一个表中的非空值更新sqlite表_Database_Sqlite_Join_Select_Left Join - Fatal编程技术网

Database 使用另一个表中的非空值更新sqlite表

Database 使用另一个表中的非空值更新sqlite表,database,sqlite,join,select,left-join,Database,Sqlite,Join,Select,Left Join,我有两个sqlite表,它们都具有相同的列布局。我希望合并这两个表(或更新表1),如果它们不为null,则从表2中获取值,否则从表1中获取值。有没有更好的方法来做到这一点,即使用多个case子句进行更新(类似于中的方法)?这些表有大量的列,这使得这样的语句相当长 |-------+-------+--------+----+--------| | id | col1 | col2 | .. | col100 | |-------+-------+--------+----+-----

我有两个sqlite表,它们都具有相同的列布局。我希望合并这两个表(或更新表1),如果它们不为null,则从表2中获取值,否则从表1中获取值。有没有更好的方法来做到这一点,即使用多个
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从表模式生成它们的方法?