如何连接xml的连接表字段

时间:2016-08-18 08:40:56

标签: sql sql-server xml postgresql postgresql-9.2

你有这样的数据结构:

public class testTable {    
   private String autoID;   
   private String docType;  
   private String alexandriaID;     

   @DynamoDBHashKey(attributeName="autoID") 
   public String getAutoID(){   return autoID;} 
   public void setAutoID(String autoID){    this.autoID = autoID;}      

   @DynamoDBRangeKey(attributeName="alexandriaID")  
   public String getAlexandraiID(){ return alexandriaID;}   
   public void setAlexandriaID(String alexandriaID){    this.alexandriaID = alexandriaID;}      

   @DynamoDBAttribute(attributeName="docType")  
   public String getDocType(){  return docType;}    
   public void setDocType(String docType){  this.docType = docType;}    

}

希望从此表中选择xml:

等数据
CREATE TEMP TABLE test_names (
  id serial primary key,
  name character varying(50),
  age int
);

INSERT INTO test_names(name,age) values ('name1',10),('name2',20);

CREATE TEMP TABLE test_names_details (
  id serial primary key,
  test_names_id int,
  col1 int,
  col2 int,
  col3 int
);

INSERT INTO test_names_details(test_names_id,col1,col2,col3)
VALUES(1,2,3,4),(1,5,6,7),(1,8,9,10),(2,20,21,22),(2,23,24,25)

怎么做?

1 个答案:

答案 0 :(得分:1)

将此功能包装在一个功能中,然后关闭! ; - )

SELECT
'<info>' || 
    string_agg(
    '<maininfo>' ||
        '<pn name="name">' || name || '</pn>' ||
        '<age name="age">' || age || '</age>' ||
    '</maininfo>' ||
    '<data>' ||
        (SELECT
            string_agg(
        '<row>' ||
            '<col name="col1">'||col1||'</col>' ||
            '<col name="col2">'||col2||'</col>' ||
            '<col name="col3">'||col3||'</col>' ||
        '</row>', '')
        FROM test_names_details WHERE test_names_id = test_names.id
        ) ||
    '</data>'
    , '') ||
'</info>'
FROM test_names