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')
相关推荐
Mr_hwt_1231 分钟前
基于MyCat 中间件实现mysql集群读写分离与从库负载均衡教程(详细案例教程)
数据库·mysql·中间件·mysql集群
酷ku的森1 小时前
Redis中的Zset数据类型
数据库·redis·缓存
zhong liu bin1 小时前
MySQL数据库面试题整理
数据结构·数据库·mysql
luckys.one5 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
言之。7 小时前
Django中的软删除
数据库·django·sqlite
阿里嘎多哈基米8 小时前
SQL 层面行转列
数据库·sql·状态模式·mapper·行转列
抠脚学代码9 小时前
Ubuntu Qt x64平台搭建 arm64 编译套件
数据库·qt·ubuntu
jakeswang9 小时前
全解MySQL之死锁问题分析、事务隔离与锁机制的底层原理剖析
数据库·mysql
Heliotrope_Sun9 小时前
Redis
数据库·redis·缓存
一成码农9 小时前
MySQL问题7
数据库·mysql