1581. 进店却未进行过交易的客户

表:Visits

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| visit_id | int |
| customer_id | int |
+-------------+---------+
visit_id是该表的主键。
该表包含有关光临过购物中心的顾客的信息。
 

表:Transactions

+----------------+---------+
| Column Name | Type |
+----------------+---------+
| transaction_id | int |
| visit_id | int |
| amount | int |
+----------------+---------+
transaction_id 是此表的主键。
该表包含有关光临过购物中心的顾客的信息。
 

编写一个 SQL 查询来查找没有进行任何交易的访问用户的 ID ,以及他们进行这些访问的次数。

返回以任何顺序排序的结果表。

解题代码:

select v.customer_id,count(*) as count_no_trans from Visits v 
where v.visit_id not in (select visit_id from Transactions)
group by v.customer_id
order by count_no_trans desc
原文地址:https://www.cnblogs.com/tomorrow-hope/p/13834339.html