从3个表中获取具有不同信息的数据

时间:2019-10-21 08:58:36

标签: mysql sql

请尊重我是SQL编程的新手。 这是我的问题,我有3个不同的表,每个表都有不同的信息

表1 :名称,用户ID,Kycid,任务ID,借出ID,摘要
表2 :用户ID,原因ID,KYCID,任务ID
表3 :原因ID,原因摘要

我想要显示以下结果:名称,用户ID,Kycid,任务ID,借贷ID,摘要,Reasonid,Reasonummary

每个原因ID都有相应的原因摘要:

示例: Reasonid:Reasonsummary
1 =请求取消
2 =年龄不合格

2 个答案:

答案 0 :(得分:0)

对于此示例数据:

create table Table1 (Name varchar(50), userid int, kycid int, taskid int, loanid int, summary int);

create table Table2 (userid int, reasonid int, kycid int, taskid int);

create table Table3 (reasonid int, reasonsummary int);

insert into Table1 values('Name1', 1, 2, 3, 4, 5);
insert into Table1 values('Name2', 2, 3, 3, 4, 5);


insert into Table2 values(1, 33, 44, 55);
insert into Table2 values(3, 66, 44, 55);


insert into Table3 values(33, 55);
insert into Table3 values(55, 55);

您可以尝试以下操作:

Select t1.Name
       , t1.userid
       , t1.kycid
       , t1.taskid
       , t1.loanid
       , t1.summary
       , t2.reasonid
       , t3.reasonsummary
from table1 t1 
left join table2 t2 on t1.userid = t2.userid
left join table3 t3 on t2.reasonid = t3.reasonid;

但是请注意,其他用户提出的所有问题都是有效的,并且在提出问题时应添加更多详细信息。希望这会有所帮助。

这是演示: http://sqlfiddle.com/#!9/5f736ce/1

答案 1 :(得分:0)

 Select t1.Name
      , t1.userid
      , t1.kycid
      , t1.taskid
      , t1.loanid
      , t1.summary
      , t2.reasonid
      , t3.reasonsummary 
 from table1 t1 
      , table2 t2
      , table3 t3
 where t1.userid=t2.userid 
 and t2.reasonid=t3.reasonid

尝试一下。