数据库查询以将数据作为列中的键值对获取

时间:2015-03-04 13:37:15

标签: sql database oracle

我的要求是我有一个名为lmss_user的表和另外两个表custom_field_name和custom _field_value现在lmss_user中的一个用户可以在custom_field_name中有多行,并且他们在custom_field_value表中有相应的值,所以我需要一个查询,它会返回所有参数lmss_user表以及custom_field_name的key,value对的列表,custom_field_value作为单行中的最后一列,当前我的查询为custom_field_name中的每一行提供了一行 -

SELECT i.EMAIL_ID,cun.NAME,cuv.VALUE
FROM lmss_user i ,
  CUSTOM_FIELD_NAME cun ,
  CUSTOM_FIELD_VALUE cuv
WHERE i.id  = cun.user_id
AND cun .id = cuv.field_name_id
AND i.id    = cuv.user_id;

CREATE TABLE custom_field_name
(
id NUMERIC(10) NOT NULL,
name VARCHAR2(100),
user_id number(8),
CONSTRAINT pk_custom_field_name_id PRIMARY KEY (id)
);

CREATE TABLE custom_field_value
(
id NUMERIC(10) NOT NULL,
value VARCHAR2(100),
user_id number(8), field_name_id number(10),
CONSTRAINT pk_custom_field_value_id PRIMARY KEY (id)
);

create table lmss_user(
    id number(8),
    active varchar2(1),
    absent varchar2(1),
    active_period varchar2(1),
  approvals number(8),
  approver varchar2(100),
    activation_date date,
    deactivation_date date,
    activePeriod_timeZone varchar2(100),    
    prefix varchar2(100),
    first_name varchar2(100),
    middle_name varchar2(100),
    last_name varchar2(100),
    suffix varchar2(100),   
    email_Id varchar2(100), 
    work_phone varchar2(100),
    home_phone varchar2(100),   
    mobile_phone varchar2(100),
    fax varchar2(100),  
    country varchar2(100),
    address_line1 varchar2(100),
    address_line2 varchar2(100),
    city varchar2(100),
    state varchar2(100),
    postal_code varchar2(100),  
    requires_approvals varchar2(100),
    month_Of_service number(8),
    status varchar2(100),   
    classification varchar2(100),
    last_hire_date date,
    original_hire_date date,    
    ethinicity  varchar2(100),
    user_name  varchar2(100),
    gender  varchar2(100),  
    allow_reconcile  varchar2(1),
    password_text varchar2(100),
  CONSTRAINT pk_user_id PRIMARY KEY (id)
)

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

SELECT i.id, i.EMAIL_ID, wmsys.wm_concat(cun.NAME||': '||cuv.VALUE) fields_and_values
  FROM lmss_user i
    join CUSTOM_FIELD_NAME cun on cun.user_id = i.id 
    join CUSTOM_FIELD_VALUE cuv on cuv.user_id = i.id and cuv.field_name_id = cun.id
  group by i.id, i.email_id

结果:

 ID EMAIL_ID       FIELDS_AND_VALUES
--- -------------- -------------------------------------
  1 john@test.com  field1: jkl,field2: mno,field3: abc
  2 anne@test.com  field4: def
  3 bill@test.com  field7: ghi

如果您使用的是Oracle 11g或更新版本,请使用listagg代替wmsys.wm_concat


编辑:Oracle 11g及更新版本:

SELECT i.id, i.EMAIL_ID, 
    listagg(cun.NAME||': '||cuv.VALUE, '   ') 
      within group (order by cun.name) fields_and_values
  FROM lmss_user i
    join CUSTOM_FIELD_NAME cun on cun.user_id = i.id 
    join CUSTOM_FIELD_VALUE cuv on cuv.user_id = i.id and cuv.field_name_id = cun.id
  group by i.id, i.email_id

结果:

ID   EMAIL_ID        FIELDS_AND_VALUES
---  --------------  ----------------------------------------------------- 
  1  john@test.com   field1: jkl   field2: mno   field3: abc
  2  anne@test.com   field4: def
  3  bill@test.com   field7: ghi