SQL- left join 与group by联合使用实例

表: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 是该表中具有唯一值的列。
此表包含 visit_id 期间进行的交易的信息。

有一些顾客可能光顾了购物中心但没有进行交易。请你编写一个解决方案,来查找这些顾客的 ID ,以及他们只光顾不交易的次数。

sql 复制代码
select customer_id,count(customer_id) as count_no_trans
from Visits v 
left join Transactions t
on v.visit_id = t.visit_id
where transaction_id is null
group by customer_id;
相关推荐
不会就选b7 小时前
MySQL之视图
数据库·mysql
>no problem<7 小时前
基于cola5.0的基础设施层的多数据库切换方案思路
数据库·spring boot·mybatisplus·cola5.0·数据库迁移适配
OceanBase数据库官方博客7 小时前
OceanBase 赋能央国企:从发电到用电的全链路业务承载
数据库·oceanbase
瀚高PG实验室8 小时前
pgsql-ogr-fdw
数据库·postgresql·瀚高数据库·highgo
IvorySQL8 小时前
PostgreSQL 技术日报 (6月5日)|PG19 Beta1 上线,PGConf.PL 2026开启征稿
数据库·postgresql·区块链
abcy0712139 小时前
pycharm python sqlalchemy mysql增删改查实例csdn
数据库·oracle
无风听海9 小时前
IndexedDB 深度指南 浏览器中的事务型对象数据库
前端·数据库
咋吃都不胖lyh10 小时前
langgraph基础示例
数据库
网管NO.110 小时前
子查询进阶|EXISTS/IN/ANY/ALL,优化查询效率
数据库·sql
云服务器租用费用11 小时前
2026年腾讯云OpenClaw(Clawdbot)+Skills云上部署及Windows本地集成轻松入门
运维·服务器·数据库·windows·云计算·腾讯云