题目链接:点击打开链接
题目大意:略。
解题思路:略。
AC 代码
--解决方案(1) WITHt1AS(SELECTcustomer_id, customer_name, COUNT(contact_name) contacts_cnt, COUNT(IF(contact_emailIN (SELECTemailFROMCustomers), 1, null)) trusted_contacts_cntFROMCustomerscuLEFTJOINContactscoONcustomer_id=user_idGROUPBYcustomer_id) SELECTinvoice_id, customer_name, price, contacts_cnt, trusted_contacts_cntFROMt1JOINInvoicesinvONinv.user_id=customer_idORDERBYinvoice_id--解决方案(2) SELECTinvoices.invoice_id, customers.customer_name, invoices.price, COUNT(contacts.user_id) AScontacts_cnt, COUNT(c2.email) AStrusted_contacts_cntFROMInvoicesinvoicesINNERJOINCustomerscustomersONinvoices.user_id=customers.customer_idLEFTJOINContactscontactsONcustomers.customer_id=contacts.user_idLEFTJOINCustomersc2ONcontacts.contact_email=c2.emailGROUPBYinvoices.invoice_idORDERBYinvoices.invoice_id