Php 在mysql中连接4个表,提供重复记录
我想在mysql查询中加入以下4个表(Lead、categories、sales\u persons、website\u Assignments): 必需:我需要潜在客户id和潜在客户记录,但不确定该潜在客户的类别、销售人员是否存在, 如果它们的类别和销售人员存在,则“无”表示为空 Lead和sales\u person direct之间没有关系,它们与第三个表绑定,即website\u assign MySql查询:Php 在mysql中连接4个表,提供重复记录,php,mysql,Php,Mysql,我想在mysql查询中加入以下4个表(Lead、categories、sales\u persons、website\u Assignments): 必需:我需要潜在客户id和潜在客户记录,但不确定该潜在客户的类别、销售人员是否存在, 如果它们的类别和销售人员存在,则“无”表示为空 Lead和sales\u person direct之间没有关系,它们与第三个表绑定,即website\u assign MySql查询: SELECT a.id, a.name, b.name, c.first_n
SELECT a.id, a.name, b.name, c.first_name, c.last_name
FROM leads a INNER JOIN sales_persons c
LEFT JOIN categories b ON a.category_id = b.id
LEFT JOIN website_assigns d ON a.id = d.lead_id and c.id = d.sales_person_id
leads:
id | name | category_id
1 xyz 12
2 abc 13
3 def 14
4 gty 15
5 wer 16
categories:
id | name
12 grocery
13 cloths
14 books
15 travel
16 furniture
sales_persons:
id | first_name | last_name
111 Jack Soloman
112 Peris Rock
113 Pull Cha
114 Tim shock
website_assigns:
id | lead_id | sales_person_id
1 1 112
2 1 114
3 2 113
4 2 111
5 3 113
6 3 112
7 3 114
上面的查询提供了Lead、category和sales person表的重复记录
表格:
SELECT a.id, a.name, b.name, c.first_name, c.last_name
FROM leads a INNER JOIN sales_persons c
LEFT JOIN categories b ON a.category_id = b.id
LEFT JOIN website_assigns d ON a.id = d.lead_id and c.id = d.sales_person_id
leads:
id | name | category_id
1 xyz 12
2 abc 13
3 def 14
4 gty 15
5 wer 16
categories:
id | name
12 grocery
13 cloths
14 books
15 travel
16 furniture
sales_persons:
id | first_name | last_name
111 Jack Soloman
112 Peris Rock
113 Pull Cha
114 Tim shock
website_assigns:
id | lead_id | sales_person_id
1 1 112
2 1 114
3 2 113
4 2 111
5 3 113
6 3 112
7 3 114
附加重复记录屏幕截图:
SELECT a.id, a.name, b.name, c.first_name, c.last_name
FROM leads a INNER JOIN sales_persons c
LEFT JOIN categories b ON a.category_id = b.id
LEFT JOIN website_assigns d ON a.id = d.lead_id and c.id = d.sales_person_id
leads:
id | name | category_id
1 xyz 12
2 abc 13
3 def 14
4 gty 15
5 wer 16
categories:
id | name
12 grocery
13 cloths
14 books
15 travel
16 furniture
sales_persons:
id | first_name | last_name
111 Jack Soloman
112 Peris Rock
113 Pull Cha
114 Tim shock
website_assigns:
id | lead_id | sales_person_id
1 1 112
2 1 114
3 2 113
4 2 111
5 3 113
6 3 112
7 3 114
请参见重复的销售人员id(潜在客户id)、姓名(类别名称)、姓名和姓氏
但48475868只分配了销售人员
在屏幕截图中,leadsite_assign与表Web站点_assign相同。
所以忽略表的名称
试试这个:
SELECT
distinct a.id, a.name, b.name, c.first_name, c.last_name
FROM leads a
LEFT JOIN
website_assigns d on a.id = d.lead_id
LEFT JOIN
sales_persons c on c.id = d.sales_person_id
LEFT JOIN
categories b ON a.category_id = b.id
输出:
id name name first_name last_name
--- ---- ------- --------- --------
1 xyz grocery Peris Rock
1 xyz grocery Tim shock
2 abc cloths Jack Soloman
2 abc cloths Pull Cha
3 def books Peris Rock
3 def books Pull Cha
3 def books Tim shock
4 gty travel NULL NULL
5 wer furniture NULL NULL
理解加入:
我们可以看到,销售线索
和销售人员
通过网站分配
表间接连接。因此,我们使用左连接将这3个表连接在一起
LEFT JOIN基本上返回左侧表中的每一行。右侧表中不匹配的行映射到NULL
categories
和leads
之间的关系是直接的,即a.categories\u id=b.id
。您的查询将精确返回它指定的内容。请提供示例数据、所需结果,并解释“重复值”的含义。请参阅:那么您需要的记录是唯一的?@GordonLinoff我附上了我的重复记录屏幕截图..完美。你能解释一下它的理解和流程吗?@希望:检查更新的答案。另见: