简体   繁体   English

使用子查询联接三个表

[英]Joining three tables using sub-query

I am trying to join 3 tables to pull out the information in the order displayed below but The Query I tried is only displaying records that exist in the lead_assign table. 我正在尝试加入3个表以按下面显示的顺序提取信息,但是我尝试的查询仅显示了lead_assign表中存在的记录。 I know I would have to do a sub query but i am not sure how it would be written. 我知道我必须做一个子查询,但是我不确定它是怎么写的。 Can someone point me in the right directions? 有人可以指出正确的方向吗? thanks 谢谢

Format I am looking for: 我正在寻找的格式:

rec_date, source, phone, email, fname lname

Table Designs: 表设计:

在此处输入图片说明

The query that I tried: 我尝试过的查询:

select
l.rec_date,
l.source,
l.name,
l.phone,
l.email,
l.comments,
u.fname,
u.lname
from leads l
inner join lead_assign la
on l.id = la.lead_id
inner join users u
on la.user_id = u.id
where l.is_deleted=0

I think you just need to use a LEFT JOIN : 我认为您只需要使用LEFT JOIN

select
   l.rec_date,
   l.source,
   l.name,
   l.phone,
   l.email,
   l.comments,
   u.fname,
   u.lname
from leads l
   left join lead_assign la
      on l.id = la.lead_id
   left join users u
      on la.user_id = u.id
where l.is_deleted=0

This will return all records from the leads table, and only display the u.fname and u.lname where matching rows exists. 这将返回Leads表中的所有记录,并且仅在存在匹配行的地方显示u.fname和u.lname。

Good luck. 祝好运。

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

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