我有两张桌子。
第一个'table_fields'
Type Name Label
ORG text1 organisation
ORG text2 location
PERS text1 manager
PERS text2 gender
PERS text3 age
第二个' table_data
'
Type ID text1 text2 text3 text4
ORG Sven HR Brussels
PERS Ludo theo male 43
ORG Sara MR Barcelona
我尝试做的是像table_Fields这样的表,它有两个额外的列,表示我的table_data的ID以及table_fields中特定Name的特定列的值。 我的结果应该是:
Type Name Label ID_table_data VALUE
ORG text1 organisation Sven HR
ORG text2 location Sven Brussels
PERS text1 manager Ludo theo
PERS text2 gender Ludo male
PERS text3 age Ludo 43
ORG text1 organisation Sara MR
ORG text2 location Sara Barcelona
我不知道SQL Oracle中是否有类似的东西
答案 0 :(得分:2)
使用CREATE TABLE table_fields ( Type, Name, Label ) AS
SELECT 'ORG', 'text1', 'organisation' FROM DUAL UNION ALL
SELECT 'ORG', 'text2', 'location' FROM DUAL UNION ALL
SELECT 'PERS', 'text1', 'manager' FROM DUAL UNION ALL
SELECT 'PERS', 'text2', 'gender' FROM DUAL UNION ALL
SELECT 'PERS', 'text3', 'age' FROM DUAL;
CREATE TABLE table_data ( Type, ID, text1, text2, text3, text4 ) AS
SELECT 'ORG', 'Sven', 'HR', 'Brussels', NULL, CAST( NULL AS VARCHAR2(20) ) FROM DUAL UNION ALL
SELECT 'PERS', 'Ludo', 'theo', 'male', '43', NULL FROM DUAL UNION ALL
SELECT 'ORG', 'Sara', 'MR', 'Barcelona', NULL, NULL FROM DUAL;
:
Oracle 11g R2架构设置:
SELECT f.*,
d.id,
d.value
FROM table_data
UNPIVOT ( value FOR name IN (
text1 AS 'text1',
text2 AS 'text2',
text3 AS 'text3',
text4 AS 'text4'
) ) d
INNER JOIN table_fields f
ON ( f.type = d.type AND f.name = d.name)
查询1 :
| TYPE | NAME | LABEL | ID | VALUE |
|------|-------|--------------|------|-----------|
| ORG | text1 | organisation | Sven | HR |
| ORG | text2 | location | Sven | Brussels |
| PERS | text1 | manager | Ludo | theo |
| PERS | text2 | gender | Ludo | male |
| PERS | text3 | age | Ludo | 43 |
| ORG | text1 | organisation | Sara | MR |
| ORG | text2 | location | Sara | Barcelona |
<强> Results 强>:
var messagebody = "Hello ".concat(req.body.name).concat(", One of your team mates have submitted an application form for intern next summer. Please approve or reject the same on the internship portal. Best Regards.");
var mailOptions = {
from: from, // sender address
to: to, // list of receiver
// cc: cc,
subject: subject, // Subject line
text: messagebody, // plaintext body
html: ' Hello '.concat(req.body.name).concat(' , <br /></br > One of your team mates have submitted an application for intern(s) for next summer. Please approve or reject the proposal on the internship portal. <br /> Here is the link of the internship portal : <a href="https://9.109.124.229:9100/"></a><br /><br /> Best Regards.') // html body
};
答案 1 :(得分:1)
您正在寻找INNER JOIN
之类的:
SELECT t2.*
,t2.ID AS ID_table_data
,CASE
WHEN t2.NAME = 'text1'
THEN d.text1
WHEN t2.NAME = 'text2 '
THEN d.text2
WHEN t2.NAME = 'text3 '
THEN d.text3
END As VALUE
FROM table_fields t1 INNER JOIN table_data t2 ON t1.Type = t2.Type
修改强> 根据我在开始时没有意识到的MT0评论,我编辑了添加CASE语句以匹配预期结果
答案 2 :(得分:1)
如果由于版本不兼容而无法使用MT0建议的UNPIVOT
(例如,您使用的Oracle版本低于11g),这可能对您有用:
with table_fields (type, name, label) as
(
select 'ORG', 'text1', 'organisation' from dual union all
select 'ORG', 'text2', 'location' from dual union all
select 'PERS', 'text1', 'manager' from dual union all
select 'PERS', 'text2', 'gender' from dual union all
select 'PERS', 'text3', 'age' from dual
),
table_data (type, id, text1, text2, text3, text4) as
(
select 'ORG', 'Sven', 'HR', 'Brussels', null, null from dual union all
select 'PERS', 'Ludo', 'theo', 'male', '43', null from dual union all
select 'ORG', 'Sara', 'MR', 'Barcelona', null, null from dual
)
select tf.type, tf.name, tf.label, td.id id_table_data,
case
when tf.name = 'text1' then td.text1
when tf.name = 'text2' then td.text2
when tf.name = 'text3' then td.text3
when tf.name = 'text4' then td.text4
else null
end value
from table_fields tf
inner join table_data td on (td.type = tf.type);