Leetcode1364. 顾客的可信联系人数量(中等)

题目
顾客表:Customers

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| customer_name | varchar |
| email         | varchar |
+---------------+---------+

customer_id 是这张表的主键。
此表的每一行包含了某在线商店顾客的姓名和电子邮件。

联系方式表:Contacts

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user_id       | id      |
| contact_name  | varchar |
| contact_email | varchar |
+---------------+---------+

(user_id, contact_email) 是这张表的主键。
此表的每一行表示编号为 use_id 的顾客的某位联系人的姓名和电子邮件。
此表包含每位顾客的联系人信息,但顾客的联系人不一定存在于顾客表中。

发票表:Invoices

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| invoice_id   | int     |
| price        | int     |
| user_id      | int     |
+--------------+---------+

invoice_id 是这张表的主键。
此表的每一行分别表示编号为 use_id 的顾客拥有有一张编号为 invoice_id、价格为 price 的发票。

为每张发票 invoice_id 编写一个SQL查询以查找以下内容:

customer_name:与发票相关的顾客名称。
price:发票的价格。
contacts_cnt:该顾客的联系人数量。
trusted_contacts_cnt:可信联系人的数量:既是该顾客的联系人又是商店顾客的联系人数量(即:可信联系人的电子邮件存在于客户表中)。
将查询的结果按照 invoice_id 排序。

查询结果的格式如下例所示:

Customers table:

+-------------+---------------+--------------------+
| customer_id | customer_name | email              |
+-------------+---------------+--------------------+
| 1           | Alice         | [email protected] |
| 2           | Bob           | [email protected]   |
| 13          | John          | [email protected]  |
| 6           | Alex          | [email protected]  |
+-------------+---------------+--------------------+

Contacts table:

+-------------+--------------+--------------------+
| user_id     | contact_name | contact_email      |
+-------------+--------------+--------------------+
| 1           | Bob          | [email protected]   |
| 1           | John         | [email protected]  |
| 1           | Jal          | [email protected]   |
| 2           | Omar         | [email protected]  |
| 2           | Meir         | [email protected]  |
| 6           | Alice        | [email protected] |
+-------------+--------------+--------------------+

Invoices table:

+------------+-------+---------+
| invoice_id | price | user_id |
+------------+-------+---------+
| 77         | 100   | 1       |
| 88         | 200   | 1       |
| 99         | 300   | 2       |
| 66         | 400   | 2       |
| 55         | 500   | 13      |
| 44         | 60    | 6       |
+------------+-------+---------+

Result table:

+------------+---------------+-------+--------------+----------------------+
| invoice_id | customer_name | price | contacts_cnt | trusted_contacts_cnt |
+------------+---------------+-------+--------------+----------------------+
| 44         | Alex          | 60    | 1            | 1                    |
| 55         | John          | 500   | 0            | 0                    |
| 66         | Bob           | 400   | 2            | 0                    |
| 77         | Alice         | 100   | 3            | 2                    |
| 88         | Alice         | 200   | 3            | 2                    |
| 99         | Bob           | 300   | 2            | 0                    |
+------------+---------------+-------+--------------+----------------------+

Alice 有三位联系人,其中两位(Bob 和 John)是可信联系人。
Bob 有两位联系人, 他们中的任何一位都不是可信联系人。
Alex 只有一位联系人(Alice),并是一位可信联系人。
John 没有任何联系人。

解答
连接Invoices和Customers 得到顾客名称
把user_id选出是为了便于后边连接

select I.invoice_id, I.user_id,C.customer_name, I.price
from Invoices as I
left join Customers as C
on C.customer_id = I.user_id
order by I.invoice_id asc;

下面需要合并 contacts_cnt:该顾客的联系人数量

select CC.user_id, count(*) as contacts_cnt
from Contacts as CC
group by CC.user_id;

连接

select tmp.invoice_id, tmp.customer_name, tmp.price, 
ifnull(tmp1.contacts_cnt, 0) as contacts_cnt
from (select I.invoice_id, I.user_id,C.customer_name, I.price
from Invoices as I
left join Customers as C
on C.customer_id = I.user_id
order by I.invoice_id asc) as tmp
left join (select CC.user_id, count(*) as contacts_cnt
from Contacts as CC
group by CC.user_id) as tmp1
on tmp.user_id = tmp1.user_id;

最后合并sted_contacts_cnt:可信联系人的数量:既是该顾客的联系人又是商店顾客的联系人数量(即:可信联系人的电子邮件存在于客户表中)。
先选出sted_contacts_cnt

select CCC.user_id, count(*) as trusted_contacts_cnt
from Contacts as CCC
where CCC.contact_name in (select customer_name from customers)
group by CCC.user_id;

最后再一次连接即可

select tmp.invoice_id, tmp.customer_name, tmp.price, 
ifnull(tmp1.contacts_cnt, 0) as contacts_cnt,
ifnull(tmp2.trusted_contacts_cnt, 0) as trusted_contacts_cnt
from (select I.invoice_id, I.user_id,C.customer_name, I.price
from Invoices as I
left join Customers as C
on C.customer_id = I.user_id
order by I.invoice_id asc) as tmp
left join (select CC.user_id, count(*) as contacts_cnt
from Contacts as CC
group by CC.user_id) as tmp1
on tmp.user_id = tmp1.user_id
left join (select CCC.user_id, count(*) as trusted_contacts_cnt
from Contacts as CCC
where CCC.contact_name in (select customer_name from customers)
group by CCC.user_id) tmp2
on tmp.user_id = tmp2.user_id;

你可能感兴趣的:(Leetcode1364. 顾客的可信联系人数量(中等))