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

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

问题描述

film表
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));
category表
CREATE TABLE category (
category_id tinyint(3) NOT NULL ,
name varchar(25) NOT NULL, last_update timestamp,
PRIMARY KEY ( category_id ));
film_category表
CREATE TABLE film_category (
film_id smallint(5) NOT NULL,
category_id tinyint(3) NOT NULL, last_update timestamp);

使用join查询方式找出没有分类的电影id以及名称
如输入为:
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’);

来源:牛客网
链接:https://www.nowcoder.com/practice/a158fa6e79274ac497832697b4b83658?tpId=82&&tqId=29781&rp=1&ru=/ta/sql&qru=/ta/sql/question-ranking

Sql语句

思路:1、嵌套查询
           2、左链接

select film_id, title
from film
where film_id not in(select film_id
                    from film_category)
select film.film_id, film.title
from film left join film_category on film.film_id = film_category.film_id
where category_id is null;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值