sql答案7-More JOIN operations

sql练习:More JOIN operations - SQLZoo

三个表的关系

movie: (id, title, yr, director, budget, gross)

actor: (id, name)

casting: (movieid, actorid, ord)

movie表:

idtitleyrdirectorbudgetgross
10003"Crocodile" Dundee II19883815800000239606210
10004'Til There Was You19974910000000

actor表:

idname
20Paul Hogan
50Jeanne Tripplehorn

casting表:

movieidactoridord
10003204
10004501

1.

List the films where the yr is 1962 [Show idtitle]

SELECT id, title
 FROM movie
 WHERE yr=1962

2.

Give year of 'Citizen Kane'.

movie: (id, title, yr, director, budget, gross)

actor: (id, name)

casting: (movieid, actorid, ord)

select yr
from movie
where title='Citizen Kane'

3.order by

List all of the Star Trek movies, include the idtitle and yr (all of these movies include the words Star Trek in the title). Order results by year.

movie: (id, title, yr, director, budget, gross)

actor: (id, name)

casting: (movieid, actorid, ord)

select id, title, yr
from movie
where title like '%Star Trek%'
order by yr

4.

What id number does the actor 'Glenn Close' have?

movie: (id, title, yr, director, budget, gross)

actor: (id, name)

casting: (movieid, actorid, ord)

select id
from actor 
where name='Glenn Close'

5.

What is the id of the film 'Casablanca'

movie: (id, title, yr, director, budget, gross)

actor: (id, name)

casting: (movieid, actorid, ord)

select id
from movie 
where title='Casablanca'

6.

Obtain the cast list for 'Casablanca'.

what is a cast list?

Use movieid=11768, (or whatever value you got from the previous question)

获取《卡萨布兰卡》的演员表。 什么是演员表? 使用movieid=11768,(或从上一个问题中获得的任何值)

movie: (id, title, yr, director, budget, gross)

actor: (id, name)

casting: (movieid, actorid, ord)

select name
from actor
where id in
  (select actorid from casting 
  where movieid in
    (select id from movie
    where title='Casablanca'))
select name from actor a 
join casting b on a.id=b.actorid 
where b.movieid=11768

7.

Obtain the cast list for the film 'Alien'

select name
from actor
where id in
  (select actorid from casting 
  where movieid in
    (select id from movie
    where title='Alien'))

或者三个表连接起来

movie: (id, title, yr, director, budget, gross)

actor: (id, name)

casting: (movieid, actorid, ord)

select name 
from movie m
join casting on(id=movieid)
join actor a on(a.id=actorid)
where title='Alien'

8.

List the films in which 'Harrison Ford' has appeared

select title
from movie m
join casting on id=movieid
join actor a on a.id=actorid
where name='Harrison Ford'

9.

List the films where 'Harrison Ford' has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]

列出哈里森·福特出演但不是主演的电影。【注:演员表中的 ord 字段给出了演员的位置。如果 ord=1,则该演员是主演】

select title
from movie m
join casting on id=movieid
join actor a on a.id = actorid
where ord!=1
and name='Harrison Ford'

10.

List the films together with the leading star for all 1962 films.

select title, name
from movie m
join casting on id=movieid
join actor a on a.id=actorid
where ord='1'
and yr='1962' 

11.group by、having

Which were the busiest years for 'Rock Hudson', show the year and the number of movies he made each year for any year in which he made more than 2 movies.

哪些年份是“罗克·赫德森”最繁忙的年份,显示每年他制作的电影数量,只包括他每年制作超过2部电影的年份。

SELECT yr,COUNT(title) FROM
  movie JOIN casting ON movie.id=movieid
        JOIN actor   ON actorid=actor.id
WHERE name='Rock Hudson'
GROUP BY yr
HAVING COUNT(title) > 2

12.join 

List the film title and the leading actor for all of the films 'Julie Andrews' played in.

列出所有朱莉·安德鲁斯参演的电影的片名和主演。

Did you get "Little Miss Marker twice"?

Julie Andrews starred in the 1980 remake of Little Miss Marker and not the original(1934).

Title is not a unique field, create a table of IDs in your subquery

你得到“小小姐马克”两次了吗?

朱莉·安德鲁斯主演了1980年重制的《小 Miss Marker》,而不是原始版本(1934年)。 标题不是唯一字段,在子查询中创建一个ID表。

SELECT title, name FROM casting
join movie m on id=movieid
join actor a on a.id=actorid
WHERE movieid in(
  select movieid from casting 
  join actor on id=actorid
  where name='Julie Andrews')
and ord=1

13.group by、having、order by

Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.

获取按字母顺序排列的演员名单,这些演员至少有15个主演角色。

select name
from actor
join casting on id=actorid
where ord=1
group by name 
having count(actorid)>=15
order by name

14.order by ... desc

List the films released in the year 1978 ordered by the number of actors in the cast, then by title.

列出1978年发行的电影,按照演员人数排序,然后按片名排序。

select title,count(actorid)
from movie 
join casting on id=movieid
where yr='1978'
group by title
order by count(actorid) desc ,title

15.distinct

List all the people who have worked with 'Art Garfunkel'.

列出所有与'阿特·加芬克尔'合作过的人。

select distinct name
from actor 
join casting on id=actorid
where name!='Art Garfunkel'
and movieid in
  (select movieid from actor
  join casting on id=actorid
  where name='Art Garfunkel')
### DB-GPT Operators in Database Management Systems In the context of database management systems, GPT (Generalized Proximity Tree) operators are specialized functions designed to enhance query processing and optimization. However, specific mention of **DB-GPT** is not directly covered within standard literature on database operations or within provided references[^1]. The term might be a conflation or misinterpretation combining concepts from databases with those emerging from advancements in generative pre-trained transformers like GPT. For clarity: - In traditional database contexts, operators refer to entities that perform actions such as selection (`σ`), projection (`π`), join (`⨝`), among others. - Generative Pre-trained Transformers (GPTs) pertain more closely to natural language processing tasks rather than direct involvement in relational algebraic operations typical in SQL-based queries. Given this distinction, it appears there may be confusion regarding terminology when referring specifically to "DB-GPT." If intending to discuss integrating AI models into database functionalities—such integration would likely involve using machine learning algorithms alongside conventional data retrieval methods but under different nomenclature. To explore how modern artificial intelligence techniques can augment existing database capabilities without invoking potentially ambiguous terms: ```sql -- Example demonstrating use of ML model predictions integrated via stored procedures CREATE PROCEDURE PredictiveQuery(IN input_data VARCHAR(255)) BEGIN -- Hypothetical call to an external service implementing advanced analytics CALL ExternalService(input_data); END; ``` This code snippet illustrates one way predictive modeling could interface with transactional processes managed by RDBMS platforms while adhering strictly to established terminologies.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值