简体   繁体   English

带子表的SQL查询

[英]SQL query with child table

I have two tables Parent and Child. 我有两个表父母和孩子。

Parent table has following columns 父表具有以下列

Parent_id, Name Parent_id,名称

Child Table has following columns 子表具有以下列

Parent_id (foreign key), Identifier, identifier_value Parent_id(外键),标识符,identifier_value

Now I want to list different parent_id with names and identifier_value(of child table) having same identifier_value for Identifier = "test" but different identifier_value for Identifier = "test1" 现在,我要列出名称和(子表的)identifier_value的不同parent_id,它们具有相同的Identifier =“ test”的identifier_value,但具有不同的Identifier =“ test1”的identifier_value

ie consider an example 即考虑一个例子

Parent Table --- 父表-

Parent_id, Name
1, XYZ
2, PQR

Child Table 子表

Parent_id, Identifier, Identifier_value
1, test, value1
1, test1, ghght 
2, test, value1
2, test1, khkhgj

output should be 输出应该是

1, XYZ, value1, ghght
2, PQR, value1, khkhgj

It should return result for the above example as in parent_id 1 and 2, test has same value but test1 has different value 它应返回上例的结果,如parent_id 1和2,test具有相同的值,但test1具有不同的值

This is what I have written till now 这是我到目前为止所写的

SELECT p.parent_id,
  p.name,
  eid.identifier_value,
  eid2.identifier_value
FROM parent p,
  child eid,
  child eid2
WHERE e.parent_id             = eid.parent_id
AND e.parent_id               = eid2.parent_id
AND eid.identifier  = 'test'
AND eid2.identifier = 'test1'
AND EXISTS
  (SELECT 1
  FROM child eid3
  WHERE eid3.identifier = 'test'
  AND eid3.value                    =eid.value
  AND eid3.parent_id             <> eid.parent_id
  AND EXISTS
    (SELECT 1
    FROM child eid4
    WHERE eid4.parent_id         = eid3.parent_id
    AND eid4.identifier = 'test1'
    AND eid3.value                 <> eid4.value
    AND eid4.identifier = eid3.identifier
    )
  )

The issue in the last subquery. 最后一个子查询中的问题。 Need some advice on it. 需要一些建议。

Is this what you are looking for? 这是你想要的?

select p.*, c1.Identifier_value, c2.Identifier_value
from parent p left join
     child c1
     on c1.parent_id = p.id and c1.identifier = 'test' left join
     child c2
     on c2.parent_id = p.id and c2.identifier = 'test2'
where c1.Identifier_value <> c2.Identifier_value;

What is the issue with the query given by gordon, Below are the results for the sample data which you have provided. gordon给出的查询有什么问题,下面是您提供的示例数据的结果。

with parent_table as
(
select 1 as parent_id, 'XYZ' as name from dual
union
select 2 as parent_id, 'PQR' as name from dual
),
child_table as 
(
select 1 as parent_id, 'test' as Identifier, 'value1' as Identifier_value from dual
union
select 1 as parent_id, 'test1' as Identifier, 'ghght' as Identifier_value from dual
union
select 2 as parent_id, 'test' as Identifier, 'value1' as Identifier_value from dual
union
select 2 as parent_id, 'test1' as Identifier, 'khkhgj' as Identifier_value from dual
)
select p.*, c1.Identifier_value, c2.Identifier_value
from parent_table p left join
     child_table c1
     on c1.parent_id = p.parent_id and c1.identifier = 'test' left join
     child_table c2
     on c2.parent_id = p.parent_id and c2.identifier = 'test1'
where c1.Identifier_value <> c2.Identifier_value;

Output same as expected output in the question. 输出与问题中的预期输出相同。

1   XYZ value1  ghght
2   PQR value1  khkhgj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM