LeetCode(数据库)- 顾客的可信联系人数量
Posted 程序员牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 顾客的可信联系人数量相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路:略。
AC 代码
-- 解决方案(1)
WITH t1 AS(SELECT customer_id, customer_name, COUNT(contact_name) contacts_cnt, COUNT(IF(contact_email IN (SELECT email FROM Customers), 1, null)) trusted_contacts_cnt
FROM Customers cu LEFT JOIN Contacts co ON customer_id = user_id
GROUP BY customer_id)
SELECT invoice_id, customer_name, price, contacts_cnt, trusted_contacts_cnt
FROM t1 JOIN Invoices inv ON inv.user_id = customer_id
ORDER BY invoice_id
-- 解决方案(2)
SELECT invoices.invoice_id, customers.customer_name, invoices.price, COUNT(contacts.user_id) AS contacts_cnt, COUNT(c2.email) AS trusted_contacts_cnt
FROM Invoices invoices
INNER JOIN Customers customers ON invoices.user_id = customers.customer_id
LEFT JOIN Contacts contacts ON customers.customer_id = contacts.user_id
LEFT JOIN Customers c2 ON contacts.contact_email = c2.email
GROUP BY invoices.invoice_id
ORDER BY invoices.invoice_id
以上是关于LeetCode(数据库)- 顾客的可信联系人数量的主要内容,如果未能解决你的问题,请参考以下文章