首页 > 试题广场 >

使用子查询的方式找出属于Action分类的所有电影对应的ti

[编程题]使用子查询的方式找出属于Action分类的所有电影对应的ti
  • 热度指数:134697 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
film表
字段 说明
film_id 电影id
title 电影名称
description 电影描述信息

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表
字段 说明
category_id 电影分类id
name 电影分类名称
last_update 电影分类最后更新时间

CREATE TABLE category  (
category_id  tinyint(3)  NOT NULL ,
name  varchar(25) NOT NULL, `last_update` timestamp,
PRIMARY KEY ( category_id ));
film_category表
字段 说明
film_id 电影id
category_id 电影分类id
last_update 电影id和分类id对应关系的最后更新时间

CREATE TABLE film_category  (
film_id  smallint(5)  NOT NULL,
category_id  tinyint(3)  NOT NULL, `last_update` timestamp);

你能使用子查询的方式找出属于Action分类的所有电影对应的title,description吗



输入描述:


输出描述:
示例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 film_category VALUES(1,1,'2006-02-14 21:07:09');
INSERT INTO film_category VALUES(2,1,'2006-02-14 21:07:09');
INSERT INTO film_category VALUES(3,6,'2006-02-14 21:07:09');

输出

ACADEMY DINOSAUR|A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies
ACE GOLDFINGER|A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China
select
    title,
    description
from film
where film_id in (
    select fc.film_id
    from category c join film_category fc
    on c.category_id=fc.category_id and c.name='Action'
)
发表于 2024-06-05 16:49:47 回复(0)
select f.title , f.description
from film f , film_category fc
where f.film_id = fc.film_id
and fc.category_id in (select category_id from category where name = 'Action')



这段SQL查询语句的主要目的是从电影数据库中检索出所有属于“Action”(动作)类别的电影的标题和描述。

具体解释如下:

选择字段:
sql
select f.title , f.description

这表示查询将返回film表中的title(电影标题)和description(电影描述)字段。

来源表:
sql
from film f , film_category fc

查询从film表和film_category表中选择数据,并且给film表起了一个别名f,给film_category表起了一个别名fc。

连接条件:
sql
where f.film_id = fc.film_id

这是一个内连接(INNER JOIN)的条件,表示我们只对那些在film表和film_category表中都有匹配的film_id的记录感兴趣。这确保了我们可以正确地匹配电影和其类别。

筛选条件:
sql
and fc.category_id in (select category_id from category where name = 'Action')

这是一个子查询。它首先从category表中选取所有名为'Action'的类别的category_id。然后,主查询使用这些category_id来过滤film_category表中的记录,只选择那些与'Action'类别相关联的电影。

综上所述,这个查询将返回所有属于“Action”类别的电影的标题和描述。

不过,值得注意的是,虽然这个查询可以工作,但使用显式的JOIN语法(如INNER JOIN)通常会使查询更易于理解和维护。以下是使用显式JOIN的等效查询:

sql
SELECT f.title, f.description
FROM film f
INNER JOIN film_category fc ON f.film_id = fc.film_id
INNER JOIN category c ON fc.category_id = c.category_id
WHERE c.name = 'Action';

这个查询使用了显式的INNER JOIN来连接三个表,并通过WHERE子句来过滤出“Action”类别的电影。这种方式可能更容易被现代的数据库开发者理解和接受。

发表于 2024-05-28 21:05:59 回复(2)
select title,description
from film join
(select  film_id
from category join film_category on category.category_id = film_category.category_id
where name = 'Action') a
on film.film_id = a.film_id
发表于 2024-05-10 11:25:45 回复(0)
select c.title,c.description from category a
join film_category b
on a.category_id=b.category_id
join film c
on b.film_id=c.film_id
where a.name='Action';

编辑于 2024-02-06 00:49:54 回复(0)
纯子查询解法,根据action电影分类,一路往上推出需要的film_id即可
select  
    title,
    description
from
    film
where   
    film_id in
    (select 
        film_id
    from    
        film_category
    where
        category_id = 
        (select 
            category_id
        from
            category
        where
            name = 'Action'))


编辑于 2024-01-11 16:26:49 回复(0)
套娃就完了,子查询嘛,套个三层就成
select title,description from film  -- 查询名称和描述信息,where限定film_id
where film_id in (
    select film_id from film_category where category_id in ( -- 查film_id,where限定film_category
        select category_id from  category where name = 'Action' -- 查film_category,where限定类别为‘Action’
    )
)
发表于 2024-01-09 22:40:58 回复(0)
select title, description
from film
where film_id in (
    select film_id
    from film_category
    where category_id in (
        select category_id
        from category
        where name='Action'
    )
)

发表于 2023-06-19 21:02:06 回复(0)
#你能使用子查询的方式找出属于Action分类的所有电影对应的title,description吗
select title,description
from film f 
join film_category fc
on f.film_id=fc.film_id where fc.category_id in(

     select category_id
     from category where name='Action'

)
     

发表于 2023-05-13 15:59:31 回复(0)
2个子查询哈
with tmp1 as(
    select category_id
    from category
    where name="Action"
)
,
tmp2 as(
    select film_id
    from film_category
    where category_id in (select category_id from tmp1)
)

select title,description
from film
where film_id in (select film_id from tmp2)


发表于 2023-03-02 16:55:13 回复(0)
参考评论区的方法总结了下
# 方法一:
select title, description
from film
where film_id in
(
    select film_id
    from category 
        join film_category using(category_id)
    where name = 'action'
);

# 方法二:
select title, description
from film
    join film_category using(film_id)
where category_id in
(
    select category_id
    from category
    where name = 'action'
);

# 方法三:
select title, description
from
(
    select title, description
    from film_category
        join film using(film_id)
        join category using(category_id)
    where name = 'action'
) as t;

# 方法四:
select title, description
from film
where film_id in
(
    select film_id
    from film_category
    where category_id in
    (
        select category_id
        from category
        where name = 'action'
    )
);


发表于 2023-02-24 00:08:20 回复(0)
select title,description
from film 
where film_id in (
    select film_id 
    from film_category
    where category_id in (
        select category_id
        from category
        where name = 'Action'
    )
)

发表于 2022-12-23 23:39:59 回复(0)
select 
    t1.title,
    t1.description
from 
    film t1 join film_category t2 on t1.film_id=t2.film_id
    join category t3 on t2.category_id=t3.category_id
where 
    t3.name='Action'
用的连表,没有用子查询,因为实际工作中我肯定会用连表方式
发表于 2022-12-10 11:52:19 回复(0)
select c.title,c.description
from
film c
inner join
(select b.film_id from category a inner join film_category b on a.category_id = b.category_id where a.name = 'Action') d
on
c.film_id = d.film_id

发表于 2022-12-02 16:33:51 回复(0)
select f.title,
f.description
from film f
where f.film_id in (select  f1.film_id 
                  from film_category f1
                  left join category c on c.category_id=f1.category_id
                  where c.name='Action') 

发表于 2022-11-04 23:28:13 回复(0)
select
  title,
  description
from
  film
where
  film_id in (
    select
      film_id
    from
      film_category
    where
      category_id in (
        select
          category_id
        from
          category
        where
          name = 'Action'
      )
  )
发表于 2022-10-14 08:56:21 回复(0)
select title,
       description
from film
where film_id in (
                 select film_id
                 from film_category
                 where category_id in (
                                      select category_id
                                      from category
                                      where name = 'Action'
                                      )
                 ) 

发表于 2022-10-04 22:05:32 回复(0)
select title, description
from film
where film_id in (select film_id
                  from film_category
                  where category_id in (select category_id
                                        from category
                                        where name = 'Action'
                                        )
                    )
发表于 2022-09-13 23:21:58 回复(0)
select
  f.title,f.description
from
  film f
where
  exists(
    select
      *
    from
      film_category fc
      left join category c using(category_id)
      where f.film_id = fc.film_id
      and c.name = 'Action'
  );

select
  f.title,f.description
from
  film f
where
  f.film_id in (
    select
      fc.film_id
    from
      film_category fc
      left join category c using(category_id)
      where c.name = 'Action'
  )
发表于 2022-09-13 20:12:22 回复(0)