题目:
ActorDirector
表:
写一条SQL查询语句获取合作过至少三次的演员和导演的 id 对 (actor_id, director_id)
示例:
建表语句:
1 create table if not exists actordirector_1050(actor_id int(3), director_id int(3),timestamp int(3) primary key); 2 truncate table actordirector_1050; 3 insert into actordirector_1050 values(1,1,0),(1,1,1),(1,1,2),(1,2,3),(1,2,4),(2,1,5),(2,1,6);
解题思路:
1 select actor_id,director_id 2 from actordirector_1050 3 group by actor_id,director_id 4 having count(actor_id) >= 3 5 -- having后也可以接count(*) >= 3
注意:
1.group by 后面接两个字段是因为要统计的是(actor_id, director_id)相同合作对数,需要两个字段才能确定一对相同。
2.分组后:相同的(actor_id,director_id)在一组里面,统计actor_id或者director_id或者count(*)或者count(1)都可以
标签:1050,director,actordirector,count,actor,力扣,MySQL,id From: https://www.cnblogs.com/liu-myu/p/17295027.html