顾客表:Customers
Column Name | Type |
---|---|
customer_id | int |
customer_name | varchar |
varchar |
联系方式表:Contacts
Column Name | Type |
---|---|
user_id | id |
contact_name | varchar |
contact_email | varchar |
发票表:Invoices
Column Name | Type |
---|---|
invoice_id | int |
price | int |
user_id | int |
为每张发票 invoice_id 编写一个查询方案以查找以下内容:
结果的格式如下例所示。
示例 1:
输入:
Customers 表:
customer_id | customer_name | |
---|---|---|
1 | Alice | alice@leetcode.com |
2 | Bob | bob@leetcode.com |
13 | John | john@leetcode.com |
6 | Alex | alex@leetcode.com |
Contacts 表:
user_id | contact_name | contact_email |
---|---|---|
1 | Bob | bob@leetcode.com |
1 | John | john@leetcode.com |
1 | Jal | jal@leetcode.com |
2 | Omar | omar@leetcode.com |
2 | Meir | meir@leetcode.com |
6 | Alice | alice@leetcode.com |
Invoices 表:
invoice_id | price | user_id |
---|---|---|
77 | 100 | 1 |
88 | 200 | 1 |
99 | 300 | 2 |
66 | 400 | 2 |
55 | 500 | 13 |
44 | 60 | 6 |
输出:
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 没有任何联系人。
SELECT one.invoice_id,
two.customer_name,
one.price,
COUNT(three.user_id) AS contacts_cnt,
IFNULL(SUM(three.contact_email = ANY(SELECT email FROM Customers)), 0) AS trusted_contacts_cnt
FROM Invoices one
LEFT JOIN Customers two ON one.user_id = two.customer_id
LEFT JOIN Contacts three on two.customer_id = three.user_id
GROUP BY one.invoice_id
ORDER BY one.invoice_id
1、根据发票找到对应顾客名称
SELECT two.customer_name
FROM Invoices one
LEFT JOIN Customers two ON one.user_id = two.customer_id
2、从输出结果看出得根据发票 invoice_id 分组且排序根据发票 invoice_id 顺序,取得 发票价格
SELECT one.invoice_id,
one.price
FROM Invoices one
GROUP BY one.invoice_id
GROUP BY one.invoice_id
3、顾客的联系人数量(Customers 顾客表对应 Contacts 联系人表)(two.customer_id = three.user_id)
SELECT COUNT(three.user_id) AS contacts_cnt
FROM Invoices one
LEFT JOIN Customers two ON one.user_id = two.customer_id
LEFT JOIN Contacts three on two.customer_id = three.user_id
GROUP BY one.invoice_id
ORDER BY one.invoice_id
4、可信联系人的数量是顾客联系人又是商店顾客的联系人数量,那就说明 Contacts 的对应联系人的邮箱存在于 Customers 表中
SELECT IFNULL(SUM(three.contact_email = ANY(SELECT email FROM Customers)), 0) AS trusted_contacts_cnt
FROM Invoices one
LEFT JOIN Customers two ON one.user_id = two.customer_id
LEFT JOIN Contacts three on two.customer_id = three.user_id
GROUP BY one.invoice_id
ORDER BY one.invoice_id
语法:筛选字段 = ANY(语句)
作用:只要 筛选字段
满足等于 语句
中的某一个值就返回True
语法:SUM(条件表达式)
作用:条件表达式如果为True,则加1,默认0(类似Count函数)