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')
相关推荐
CV工程师的自我修养16 分钟前
你的SQL为什么慢?看懂MySQL EXPLAIN执行计划,快速定位性能瓶颈
数据库·mysql
一壶纱18 分钟前
UniApp + Pinia 数据持久化
前端·数据库·uni-app
小宇的天下20 分钟前
Calibre 3Dstack --每日一个命令day7【Centers】(3-7)
java·服务器·数据库
y_想不到名字31 分钟前
MySQL windows版本免安装
数据库·mysql
萧曵 丶32 分钟前
MySQL 事务隔离级别及实际业务问题详解
数据库·mysql
lalala_lulu34 分钟前
什么是事务,事务有什么特性?
java·开发语言·数据库
_清欢l41 分钟前
Dify+test2data实现自然语言查询数据库
数据库·人工智能·openai
哥只是传说中的小白1 小时前
Nano Banana Pro高并发接入Grsai Api实战!0.09/张无限批量生成(附接入实战+开源工具)
开发语言·数据库·ai作画·开源·aigc·php·api
南知意-1 小时前
Navicat 17 下载安装教程!
数据库·mysql·navicat·数据库连接
l1t1 小时前
DeepSeek辅助编写的利用唯一可选数求解数独SQL
数据库·sql·算法·postgresql