2017-02-24 29 views
1

SQLヘルプが必要です。私は貿易によってJavaの男です、そして、私は本当にこの質問をする方法さえ知らない。私は3つのテーブルを持っている、それらを人、子供、友達と呼んでください。人はIDと名前です:SQL外部結合テーブル2つのテーブル1つ

| id | name | 
--------------------- 
| 1 | Joe  | 

レッツは、子供たちが同じであると言うが、バックの人にFK

| id | personId | name | 
------------------------------------- 
| 1 |  1  | Frank | 
| 2 |  1  | Dan | 

や友人と明らかに

| id | personId | name | 
------------------------------------- 
| 1 |  1  | Will | 
| 2 |  1  | Bob | 

これと同じものです私の本当の問題の簡略版ですが、構造は同じです。私はテクニックに参加このバック

| personId | personName | childId | childName | friendId | friendName 
------------------------------------------------------------------------------------------ 
|  1  | Joe  |  1  | Frank | null  | null 
|  1  | Joe  |  1  | Dan  | null  | null 
|  1  | Joe  | null | null |  1  | Will 
|  1  | Joe  | null | null |  2  | Bob 

私が試してみました、複数を得るように、1つのSQLプルで、このデータのすべてをプルする必要があるが、それをクラックに見えることはできません。 SQLは決して最善の問題ではありませんでした。今、私は明らかに、これはあまりにも動作します友人や子供たちの一覧<>とJavaオブジェクトの人にこれを解析しています。これを構築するために私のコードでは、forループきれいにできるようになります

| personId | personName | childId | childName | friendId | friendName 
------------------------------------------------------------------------------------------ 
|  1  | Joe  | null | null | null  | null 
| null | null  |  1  | Frank | null  | null 
| null | null  |  1  | Dan  | null  | null 
| null | null  | null | null |  1  | Will 
| null | null  | null | null |  2  | Bob 

何かを。

ありがとうございます!

+0

を私は専門家だが、2つの完全外部に加入person.id = children.personId 10あなたはこれをacheiveするLEFT JOINを使用することができますperson.id = friends.personIdが動作するはずです。あなたは何を試しましたか、どんな結果が得られましたか? – mreff555

+0

@ mreff555これはタグが付けられていますMySQL – Strawberry

答えて

4
select 
    p.id as personId 
    , p.name as personName 
    , c.id as childId 
    , c.name as childName 
    , null as friendId 
    , null as friendName 
from person p 
    inner join child c 
    on p.id = c.personId 
union all 
select 
    p.id as personId 
    , p.name as personName 
    , null as childId 
    , null as childName 
    , f.id as friendId 
    , f.name as friendName 
from person p 
    inner join friend f 
    on p.id = f.personId; 

rextester :http://rextester.com/BSPEC33394

リターン:

+----------+------------+---------+-----------+----------+------------+ 
| personId | personName | childId | childName | friendId | friendName | 
+----------+------------+---------+-----------+----------+------------+ 
|  1 | joe  | 1  | frank  | NULL  | NULL  | 
|  1 | joe  | 2  | dan  | NULL  | NULL  | 
|  1 | joe  | NULL | NULL  | 1  | will  | 
|  1 | joe  | NULL | NULL  | 2  | bob  | 
+----------+------------+---------+-----------+----------+------------+ 
+0

内部結合はnullを返しますか?私はあなたの代わりに左側の外側の結合を使用する必要があると思う – bksi

+0

@bksi rextesterリンクをクリックし、それをチェックして参照してください。 http://rextester.com/BSPEC33394 – SqlZim

1

あなたは外側の子供や友人の組合と合流した後、あなたは何(case whenを使用して)各列の出力に決定することで一致している両方のどのチェックできます

select person.id, 
      person.name, 
      case when rel.kind = 1 then rel.id end as childId, 
      case when rel.kind = 1 then rel.name end as childName, 
      case when rel.kind = 2 then rel.id end as friendId, 
      case when rel.kind = 2 then rel.name end as friendName 
from  person 
left join (
      select id, personId, name, 1 as kind 
      from children 
      union all 
      select id, personId, name, 2 as kind 
      from friends 
     ) as rel 
     on rel.personId = person.id 
order by person.id, 
      rel.kind 
      rel.id 
0
SELECT 
    p.id as personId 
    , p.name as personName 
    , c.id as childId 
    , c.name as childName 
    , f.id as friendId 
    , f.name as friendName 
FROM person p 
LEFT OUTER JOIN child c ON p.id = c.personId 
LEFT OUTER JOIN friend f ON p.id = f.personId; 
左用

詳細情報は、あなたがここで読むことができます結合します: https://www.w3schools.com/sql/sql_join_left.asp

関連する問題