SQL之group by连接2个以上字段

group by连接1个字段:

1.select后接group by后面的字段

2.顺序:where----group by-----order by------having

  1. select 后面的所有列中,没有使用聚合函数的列,必须出现在 group by 后面

group by 后连接2个字段:

一个字段就依据一个条件分组,两个字段就依据两个条件分组。。。。。。

不满足另外分为1组。

力扣刷题:1050合作过至少三次的演员和导演

ActorDirector 表:

复制代码
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| actor_id    | int     |
| director_id | int     |
| timestamp   | int     |
+-------------+---------+
在 SQL 中,timestamp 是这张表的主键.
查询合作过至少三次的演员和导演的 id 对 (actor_id, director_id)

答案:

sql 复制代码
select actor_id,director_id from actordirector
group by actor_id,director_id
having count(*)>=3

表结构

sql 复制代码
Create table If Not Exists ActorDirector (actor_id int, director_id int, timestamp int)
Truncate table ActorDirector
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '1', '0')
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '1', '1')
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '1', '2')
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '2', '3')
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '2', '4')
insert into ActorDirector (actor_id, director_id, timestamp) values ('2', '1', '5')
insert into ActorDirector (actor_id, director_id, timestamp) values ('2', '1', '6')
相关推荐
薛定谔的算法12 小时前
phoneGPT:构建专业领域的检索增强型智能问答系统
前端·数据库·后端
Databend14 小时前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术15 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Raymond运维19 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉19 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence2 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger2 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql