首页 > 试题广场 >

使用join查询方式找出没有分类的电影id以及名称

[编程题]使用join查询方式找出没有分类的电影id以及名称
  • 热度指数:175335 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
现有电影信息表film,包含以下字段:
字段 说明
film_id 电影id
title 电影名称
description 电影描述信息

有类别表category,包含以下字段:
字段 说明
category_id 电影分类id
name 电影分类名称
last_update 电影分类最后更新时间

电影分类表film_category,包含以下字段:
字段 说明
film_id 电影id
category_id 电影分类id
last_update 电影id和分类id对应关系的最后更新时间

film 表

film_id title description
1 ACADEMY DINOSAUR A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies
2 ACE GOLDFINGER A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China
3 ADAPTATION HOLES A Astounding Reflection of a Lumberjack And a Car who must Sink a Lumberjack in A Baloon Factory

category 表

category_id name last_update
1 Action 2006-02-14 20:46:27
2 Animation 2006-02-14 20:46:27
3 Children 2006-02-14 20:46:27
4 Classics 2006-02-14 20:46:27
5 Comedy 2006-02-14 20:46:27
6 Documentary 2006-02-14 20:46:27
7 Drama 2006-02-14 20:46:27
8 Family 2006-02-14 20:46:27
9 Foreign 2006-02-14 20:46:27
10 Games 2006-02-14 20:46:27
11 Horror 2006-02-14 20:46:27

film_category 表

film_id category_id last_update
1 6 2006-02-14 21:07:09
2 11 2006-02-14 21:07:09

使用join查询方式找出没有分类的电影id以及其电影名称。
输出:
3
ADAPTATION HOLES
示例1

输入

drop table if exists  film ;
drop table if exists  category  ; 
drop table if exists  film_category  ; 
CREATE TABLE IF NOT EXISTS film (
  film_id smallint(5)  NOT NULL DEFAULT '0',
  title varchar(255) NOT NULL,
  description text,
  PRIMARY KEY (film_id));
CREATE TABLE category  (
   category_id  tinyint(3)  NOT NULL ,
   name  varchar(25) NOT NULL, `last_update` timestamp,
  PRIMARY KEY ( category_id ));
CREATE TABLE film_category  (
   film_id  smallint(5)  NOT NULL,
   category_id  tinyint(3)  NOT NULL, `last_update` timestamp);
INSERT INTO film VALUES(1,'ACADEMY DINOSAUR','A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies');
INSERT INTO film VALUES(2,'ACE GOLDFINGER','A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China');
INSERT INTO film VALUES(3,'ADAPTATION HOLES','A Astounding Reflection of a Lumberjack And a Car who must Sink a Lumberjack in A Baloon Factory');

INSERT INTO category VALUES(1,'Action','2006-02-14 20:46:27');
INSERT INTO category VALUES(2,'Animation','2006-02-14 20:46:27');
INSERT INTO category VALUES(3,'Children','2006-02-14 20:46:27');
INSERT INTO category VALUES(4,'Classics','2006-02-14 20:46:27');
INSERT INTO category VALUES(5,'Comedy','2006-02-14 20:46:27');
INSERT INTO category VALUES(6,'Documentary','2006-02-14 20:46:27');
INSERT INTO category VALUES(7,'Drama','2006-02-14 20:46:27');
INSERT INTO category VALUES(8,'Family','2006-02-14 20:46:27');
INSERT INTO category VALUES(9,'Foreign','2006-02-14 20:46:27');
INSERT INTO category VALUES(10,'Games','2006-02-14 20:46:27');
INSERT INTO category VALUES(11,'Horror','2006-02-14 20:46:27');

INSERT INTO film_category VALUES(1,6,'2006-02-14 21:07:09');
INSERT INTO film_category VALUES(2,11,'2006-02-14 21:07:09');

输出

3|ADAPTATION HOLES
select t1.film_id, title
from film t1 left join film_category t2
on t1.film_id = t2.film_id
where t2.category_id is null

发表于 2022-09-01 17:27:16 回复(0)
select a.film_id, a.title
from film a 
left join film_category b on  a.film_id = b.film_id
where b.film_id is  null
发表于 2022-04-15 19:03:52 回复(0)
select f.film_id,f.title
from film f
left join film_category fc
on f.film_id = fc.film_id
where fc.film_id is null

--来自在深圳居家办公的果果果子2022年3月14日15:19
发表于 2022-03-14 15:19:20 回复(0)
select fl.film_id ,fl.title
from film fl
left join film_category fc
on fl.film_id = fc.film_id
where fc.category_id isnull

使用left join 会直接显示
发表于 2022-03-10 16:43:57 回复(0)
select film_id, title from film where film_id in
(select f.film_id from film as f
except
select fc.film_id from film_category as fc)
不需要join,直接找film和film_category的差集
发表于 2022-02-24 19:30:15 回复(0)
select a.film_id,a.title
from film a left join film_category b 
on a.film_id=b.film_id
where b.category_id is null
左连接film和category_id得出所有film_id,再用is null筛去已分类的film_id。
发表于 2022-02-23 15:27:12 回复(0)
select f.film_id, f.title
from film f left join film_category c
on f.film_id = c.film_id
where c.category_id is NULL
发表于 2022-02-06 00:04:39 回复(0)
给了3个表,只用到2个,以为自己理解错了,原来是我想复杂了。

SELECT a.film_id,a.title
FROM film a
LEFT JOIN film_category  b ON a.film_id=b.film_id
WHERE b.category_id IS NULL


发表于 2021-10-13 08:57:17 回复(0)
select f.film_id,f.title
from film f
    left join film_category fc on f.film_id=fc.film_id
    left join category c on fc.category_id=c.category_id
where c.category_id is null;
1.多表左连接:left join
2.找出没有分类的电影:where str is null

发表于 2021-10-11 16:30:22 回复(0)
select DISTINCT f.film_id,f.title
from category c join film_category f2 on c.category_id=f2.category_id
join film f
where f.film_id not in (select film_id from film_category)
发表于 2021-09-12 09:49:32 回复(0)
select f.film_id,f.title
from film f
left join film_category fc
on f.film_id=fc.film_id
left join category c
on c.category_id=fc.category_id
where C.name IS NULL

反正就笨办法,我觉得可以,哈哈哈哈

发表于 2021-08-06 08:52:53 回复(0)
select f.film_id, f.title 
from film f
where f.film_id in (
//二:找出没有关联上分类的电影id
select film_id
from film 
where film_id not in
(
//一:找出关联到分类的电影id
select a.film_id
from film a 
join film_category b on a.film_id = b.film_id)
)
发表于 2021-07-18 23:41:29 回复(0)