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')
相关推荐
东小黑4 分钟前
WordPress问题
数据库·wordpress
2401_879693875 分钟前
用Python批量处理Excel和CSV文件
jvm·数据库·python
gjc59225 分钟前
踩坑实录:MySQL服务器CPU爆高,元凶竟是SELinux的setroubleshootd?
运维·服务器·数据库·mysql·adb
2401_8463416527 分钟前
Python Lambda(匿名函数):简洁之道
jvm·数据库·python
2401_8796938730 分钟前
进阶技巧与底层原理
jvm·数据库·python
知识分享小能手32 分钟前
Redis入门学习教程,从入门到精通, Redis Stack 完整语法知识点及使用指南(7)
数据库·redis·学习
小仓桑37 分钟前
【Agent智能体项目实战三】LangChain调用通义千问保姆级教程
数据库·阿里云·langchain·agent
GIS阵地44 分钟前
QgsDataSourceUri解析
数据库·c++·qt·开源软件·qgis
yunyun321231 小时前
自动化与脚本
jvm·数据库·python
暮冬-  Gentle°1 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python