并且有三列(id [type-Number],name [type-Varchar2],detail [type-XMLTYPE]。详细列包含xml格式的客户地址,包括他的姓名,与第二列名称相同。
第一个 sql:工作正常[两列的名称相同]
SELECT id, name, extractValue(detail, '/customer/address[@type=''HOME'']/name1') name1 FROM ( SELECT inner1.*, (SELECT detail FROM customer c1 WHERE c1.id = inner1.id) detail FROM (SELECT c.id, c.name FROM customer c ) inner1 )
输出 :
1 Janice Janice 2 Rita Rita 3 Cory Cory
第二个 sql [无法正常工作:name1 对于第一行的所有行都是重复的]
SELECT id, name, extractValue(detail, '/customer/address[@type=''HOME'']/name1') name1, extractValue(detail, '/customer/address[@type=''HOME'']/name2') name2 FROM ( SELECT inner1.*, (SELECT detail FROM customer c1 WHERE c1.id = inner1.id) detail FROM (SELECT c.id, c.name FROM customer c) inner1 )
输出 :
1 Janice Janice Dunn 2 Rita Janice Hale 3 Cory Janice Jones
问题:在第二个 sql 中,我只从详细列中请求了额外的数据 name2,这完全改变了输出,因为您可以在第二个 sql 的输出中看到 name1 的重复数据。这种行为是如何可能的,可能的解决方案是什么?
如果我不在 sql 的 xpath 中使用属性,则问题不存在。sql 仍然有效并提供正确的数据。 喜欢
extractValue(detail, '/customer/address/name1') name1,
细节
- Oracle 数据库 11g 版本 11.2.0.3.0 - 64 位生产
客户.sql
创建表“客户”
( "ID" NUMBER, "NAME" VARCHAR2(20 BYTE), "DETAIL" "SYS"."XMLTYPE" ) XMLTYPE COLUMN "DETAIL" STORE AS SECUREFILE BINARY XML ( TABLESPACE "USERS" ENABLE STORAGE IN ROW CHUNK 8192 CACHE NOCOMPRESS KEEP_DUPLICATES STORAGE(INITIAL 106496 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)) ALLOW NONSCHEMA DISALLOW ANYSCHEMA ; Insert into CUSTOMER (ID,NAME,DETAIL) values (1,'Janice','<?xml version = ''1.0''?><customer> <customerno>1</customerno> <address type="HOME"> <name1>Janice</name1> <name2>Dunn</name2> <email>janice.dunn98@example.com</email> <dob>5/2/1980</dob> <city>Barn St</city> </address> </customer>'); Insert into CUSTOMER (ID,NAME,DETAIL) values (2,'Rita','<?xml version = ''1.0''?><customer> <customerno>2</customerno> <address type="HOME"> <name1>Rita</name1> <name2>Hale</name2> <email>rita.hale40@example.com</email> <dob>2/2/1981</dob> <city>Seventh St</city> </address> </customer>'); Insert into CUSTOMER (ID,NAME,DETAIL) values (3,'Cory','<?xml version = ''1.0''?><customer> <customerno>3</customerno> <address type="HOME"> <name1>Cory</name1> <name2>Jones</name2> <email>cory.jones89@example.com</email> <dob>9/7/1984</dob> <city>Arther St</city> </address> </customer>');