Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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
Sql Oracle在具有多个值的列上合并_Sql_Oracle_Sql Merge - Fatal编程技术网

Sql Oracle在具有多个值的列上合并

Sql Oracle在具有多个值的列上合并,sql,oracle,sql-merge,Sql,Oracle,Sql Merge,我在oracle数据库中有两个表 "persons" with columns (personid, lastname, firstname, address, city) "person_details" with columns (id, personid, mobileno) 和一个合并表 "merge_test" with columns (personid, detail_id, lastname, firstname, address, city, mobileno) 我使用下面

我在oracle数据库中有两个表

"persons" with columns (personid, lastname, firstname, address, city)
"person_details" with columns (id, personid, mobileno)
和一个合并表

"merge_test" with columns (personid, detail_id, lastname, firstname, address, city, mobileno)
我使用下面的merge语句

merge into merge_test
using ( select p.personid personid,
            d.id detailid,
            p.firstname firstname,
            p.lastname lastname,
            p.address address,
            p.city city,
            d.mobileno mobileno
        from persons p
            left outer join person_details d
            on p.personid = d.personid ) source
on ( 
  merge_test.personid = source.personid
  and nvl(merge_test.detail_id, nvl(source.detailid, 0)) = nvl(source.detailid, 0)
)
WHEN MATCHED THEN
  UPDATE SET 
    merge_test.firstname = source.firstname,
    merge_test.lastname = source.lastname,
    merge_test.address = source.address,
    merge_test.city = source.city,
    merge_test.mobileno = source.mobileno
WHEN NOT MATCHED THEN
  INSERT (personid, detail_id, lastname, firstname, address, city, mobileno)
  values (source.personid, source.detailid, source.lastname, source.firstname, source.address, source.city, source.mobileno);
在这里,merge_test.detail_id最初将为null,并且可以为persons.personid获取多个值(单个merge_test.personid有多行),merge语句将每小时运行一次

如果source.detailid的值得到更新,并且为source.detailid的多行插入新行,则我希望以这样的方式合并merge_test.detail_id的值得到更新

编辑13-03-2020

上面的查询将每小时运行一次,并使用merge query更新merge_测试

对于包含以下数据的persons表:

personid, lastname, firstname, address, city
1, 'lname', 'fname', 'add', 'city'
2, 'lname', 'fname', 'add', 'city'
3, 'lname', 'fname', 'add', 'city'
id, personid, mobileno
1, 1, 'mobileno'
2, 1, 'mobileno'
3, 2, 'mobileno'
对于包含以下数据的person_details表:

personid, lastname, firstname, address, city
1, 'lname', 'fname', 'add', 'city'
2, 'lname', 'fname', 'add', 'city'
3, 'lname', 'fname', 'add', 'city'
id, personid, mobileno
1, 1, 'mobileno'
2, 1, 'mobileno'
3, 2, 'mobileno'
将产生如下所示的合并测试表

personid, detail_id, lastname, firstname, address, city, mobileno
1, 1, 'lname', 'fname', 'add', 'city', 'mobileno'
1, 2, 'lname', 'fname', 'add', 'city', 'mobileno'
2, 3, 'lname', 'fname', 'add', 'city', 'mobileno'
3, null, 'lname', 'fname', 'add', 'city', 'mobileno'
上面的数据是用select语句和left outer join生成的

当person_详情表通过以下细节更新时

id, personid, mobileno
1, 1, 'mobileno'
2, 1, 'mobileno'
3, 1, 'mobileno'
4, 3, 'mobileno'
合并测试表应更新为

personid, detail_id, lastname, firstname, address, city, mobileno
1, 1, 'lname', 'fname', 'add', 'city', 'mobileno'
1, 2, 'lname', 'fname', 'add', 'city', 'mobileno'
1, 3, 'lname', 'fname', 'add', 'city', 'mobileno'
2, null, 'lname', 'fname', 'add', 'city', 'mobileno'
3, 4, 'lname', 'fname', 'add', 'city', 'mobileno'

我希望查询能够处理这种数据操作,并将数据存储在merge_测试表中

更新的答案

删除之后,您将有一个更新,将MERGE\u测试表中person\u detail表中缺少条目的记录设置为null

为了确保中间答案的值如下所示

personid, detail_id, lastname, firstname, address, city, mobileno
1, 1, 'lname', 'fname', 'add', 'city', 'mobileno'
1, 2, 'lname', 'fname', 'add', 'city', 'mobileno'
2, 3, 'lname', 'fname', 'add', 'city', 'mobileno'
3, null, 'lname', 'fname', 'add', 'city', null /*This should be null?*/
我相信您希望在on子句中丢失nvl逻辑,并在合并后包含delete语句,如下所示,以满足您所讨论的情况

(您的合并查询)


不是person\u details表中的主键组合(detail\u id,person\u id)。在这种情况下,详细信息_id如何可能得到“更新”。您可能正在更新与person_details ie(mobileno)关联的属性。我的理解正确吗?是的,问题是personid在person\u details表中的第一行不存在。几个小时后,将在person_details中为personid插入一行,所以带有左侧外部join语句的表将生成更新了detail_id的行。并且可以在person_details表中为单个personid添加多行。因此,是否希望第一次和以后使用空值创建记录要删除这些空值并仅使用详细信息的填充值,我希望第一次存储空值,然后使用select语句中的传入值更新空值,如果出现多个值,则创建一个新行,而不是更新iti。如果person_details Table中不存在值,则还希望存储空值。如果person_details Table中不存在值,则不会删除空值,除非此人具有非空详细信息idI。在从我的合并查询中删除NVL后,我尝试运行此删除查询,它不会删除任何空行,并且会为特定的person_id添加非空行。问题是,如果从person_details表中删除了行,则非空的detailid也可以设置为空,因此需要处理这一点,即expectationyes,需要将detail_id作为可更新字段处理,该字段可以包含null,个人id的一个值或多个值。