2017-06-21 83 views
-4

我有3个表,结果表应该有所有三个表中的列基于第一个表的列user_idsql查询从3个表中分离值,基于一列值

enter image description here

+0

请正确格式化您的问题以提高可读性。 –

+1

Oracle或Mysql?为什么有两个用户具有相同的user_id(10)? – Nitish

+0

MYSQL ..对不起,您可能会考虑单一时间 – karan

回答

1

假设10,约翰应为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); 

这个查询

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) 

如果模型看起来像这样

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.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)只出现一次,但在表2 u_id(10)出现2times和列temp有两个不同的值为u_id(10)和在表3 u_id发生3倍所以输出即将得到的结果集计数2 * 3 = 6为u_id(10)..正确的结果应为3,而不是6为u_id(10)..如何做到这一点。谢谢, – karan

+0

伟大的帮助谢谢..现在我必须将两列中的字符串连接成一列,它应该是不同的,基于u_id ..(例如) | u_id |名称| + ------ + ------- + | 10 |公羊〜32-25 |这是为甲骨文....这将是一个很大的帮助,等待你的答复谢谢。 – karan

+0

即时通讯使用LISTAGG功能,并获得输出..但无法满足我的要求concatenate 2列..请尝试解决这个问题。 – karan

相关问题