2017-06-21 14 views
-4

3つのテーブルがあり、結果テーブルには、最初のテーブルの列user_idに基づいて3つのテーブルすべてからの列が必要です。 10と仮定すると1つの列の値に基づいて3つのテーブルから値を分離するSqlクエリ

enter image description here

+0

読みやすくするために、質問の形式を正しく指定してください。 –

+1

OracleまたはMysql?なぜ同じuser_id(10)の2人のユーザーがいるのですか? – Nitish

+0

MYSQL ..ごめんなさい。一度にご検討ください。 – karan

答えて

1

非常に単純結合のように、ジョンは15である必要があり、ジョンはこれが見えます。下のモデルが間違っている場合は、コピーして編集して質問に追加してください。

drop table if exists t1,t2,t3; 

create table t1(u_id int, name varchar(10), prd int); 
create table t2(u_id int, temp int); 
create table t3(u_id int, Office int); 

insert into t1 values (10,'ram',1), (15,'john',1),(20,'sat',1),(12,'peter',1); 
insert into t2 values (10,32),(20,42),(15,25),(12,32); 
insert into t3 values (20,56),(10,57),(15,56),(12,57); 

モデルはあなたがフル参加

をシミュレートする必要があり、この

drop table if exists t1,t2,t3; 

create table t1(u_id int, name varchar(10), prd int); 
create table t2(u_id int, temp int); 
create table t3(u_id int, Office int); 

insert into t1 values (10,'ram',1), (15,'john',1),(20,'sat',1),(12,'peter',1); 
insert into t2 values (10,32),(20,42),(10,25),(12,32); 
insert into t3 values (20,56),(10,57),(10,60),(10,56),(12,57); 

のように見える場合は、このクエリ

select t1.*,t2.*,t3.* 
from t1 
join t2 on t2.u_id = t1.u_id 
join t3 on t3.U_id = t1.u_id 
order by t1.u_id; 

この結果

+------+-------+------+------+------+------+--------+ 
| u_id | name | prd | u_id | temp | u_id | Office | 
+------+-------+------+------+------+------+--------+ 
| 10 | ram | 1 | 10 | 32 | 10 |  57 | 
| 12 | peter | 1 | 12 | 32 | 12 |  57 | 
| 15 | john | 1 | 15 | 25 | 15 |  56 | 
| 20 | sat | 1 | 20 | 42 | 20 |  56 | 
+------+-------+------+------+------+------+--------+ 
4 rows in set (0.00 sec) 

を生成します

select t1.u_id,t1.name,t2temp,t3office 
from t1 
join 
(
select t2.u_id as t2id, t2.temp as t2temp, t3.u_id as t3uid,t3.office as t3office 
from 
(
select t2.*,if(t2.u_id <> @p ,@rn:=1,@rn:[email protected]+1) rn,@p:=t2.u_id from t2 ,(select @rn:=0,@p:=0) rn order by t2.u_id 
) t2 
left outer join 
(
select t3.*,if(t3.u_id <> @p1 ,@rn1:=1,@rn1:[email protected]+1) rn,@p1:=t3.u_id from t3 ,(select @rn1:=0,@p1:=0) rn1 order by t3.u_id 
) t3 on t3.u_id = t2.u_id and t2.rn = t3.rn 
union all 
select t2.u_id as t2id, t2.temp as t2temp, t3.u_id as t3uid,t3.office as t3office 
from 
(
select t2.*,if(t2.u_id <> @p ,@rn:=1,@rn:[email protected]+1) rn,@p:=t2.u_id from t2 ,(select @rn:=0,@p:=0) rn order by t2.u_id 
) t2 
right outer join 
(
select t3.*,if(t3.u_id <> @p1 ,@rn1:=1,@rn1:[email protected]+1) rn,@p1:=t3.u_id from t3 ,(select @rn1:=0,@p1:=0) rn1 order by t3.u_id 
) t3 on t3.u_id = t2.u_id and t2.rn = t3.rn 
where t2.rn is null 
) z on t1.u_id = t2id or t1.u_id = t3uid 
order by t1.u_id  

+------+-------+--------+----------+ 
| u_id | name | t2temp | t3office | 
+------+-------+--------+----------+ 
| 10 | ram | NULL |  56 | 
| 10 | ram |  32 |  57 | 
| 10 | ram |  25 |  60 | 
| 12 | peter |  32 |  57 | 
| 20 | sat |  42 |  56 | 
+------+-------+--------+----------+ 
5 rows in set (0.00 sec) 
+0

ありがとう..私は結果を得ることができます..問題があります。テーブル1のu_id(10)は1回だけ発生しますが、テーブル2のu_id(10)は2回発生し、カラムの温度はu_id(10)とテーブル3のu_id 3倍になるので、出力の取得には結果セットカウントが2 * 3 = 6、u_id(10)になります。正しい結果は、3のカウントでなければならず、u_id(10)では6ではありません。ありがとう、 – karan

+0

大きな助けていただきありがとう..今私は1列に2つの列の文字列を連結する必要があります、それは、u_id ..に基づいて別れている必要があります(つまり) | u_id |名前| + ------ + ------- + | 10 |ラム〜32-25 |それはオラクルのためのものです....あなたの返事をお待ちしております。 – karan

+0

私はLISTAGG関数を使用して出力を取得していますが、2カラムを連結するという私の要求を満たすことはできません。問題を解決しようとしてください。 – karan

関連する問題