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')
相关推荐
倔强的石头_10 小时前
《Kingbase护城河》——猎捕慢查询:执行计划的微观解析与索引调优实战
数据库
SelectDB12 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
jiayou642 天前
KingbaseES 表级与列级加密完全指南
数据库·后端
GBASE2 天前
G术时刻 |GBase 8s数据库事务并发控制之封锁技术介绍(下)
数据库
xiezhr3 天前
逛GitHub发现了一款免费的带AI功能的数据库管理工具
数据库·ai编程·dba
唐青枫4 天前
MySQL JSON 实战详解:从存储、查询、更新到 JSON_TABLE 与索引
sql·mysql
吃糖的小孩4 天前
给 QQ AI 机器人设计“可控记忆”:会话摘要、手动长期记忆与角色卡边界
数据库
笃行3505 天前
金仓数据库数据安全双防线:静态存储加密与传输加密实战
数据库
笃行3505 天前
金仓数据库物理备份实战:sys_rman 全流程演练与误覆盖抢救
数据库
笃行3505 天前
金仓数据库逻辑备份实战:从全库导出到 Schema 替换的完整闭环
数据库