日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql begin operations_MySQL入门(七):More JOIN operations

發布時間:2025/3/8 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql begin operations_MySQL入门(七):More JOIN operations 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Sqlzoo習題練習:More JOIN operations

下面會涉及到更多連接的概念。數據庫由三個表組成:movie , actor 和 casting以及三個表之間的關系。

下面為More JOIN 習題內容:

--#1/*List the films where the yr is 1962 [Show id, title]*/

SELECT id, title

FROM movie

WHERE yr=1962;

--#2/*Give year of 'Citizen Kane'.*/

SELECT yr

FROM movie

WHERE title = 'Citizen Kane';

--#3/*List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Star Trek in the title). Order results by year.*/

SELECT id,title,yr

FROM movie

WHERE title LIKE 'Star Trek%';

--#4/*What id number does the actor 'Glenn Close' have?*/

SELECT id FROM actor

WHERE name = 'Glenn Close';

--#5/*What is the id of the film 'Casablanca'*/

SELECT id

FROM movie

WHERE title = 'Casablanca';

--#6/*Obtain the cast list for 'Casablanca'.what is a cast list?The cast list is the names of the actors who were in the movie.Use movieid=11768, (or whatever value you got from the previous question)*/

SELECT actor.name

FROM actor JOIN casting ON (actor.id=casting.actorid)

WHERE casting.movieid = 11768;

--#7/*Obtain the cast list for the film 'Alien'*/

SELECT actor.name

FROM actor JOIN casting

ON (actor.id=casting.actorid)

JOIN movie

ON (movie.id = casting.movieid)

WHERE movie.title = 'Alien' ;

--#8/*List the films in which 'Harrison Ford' has appeared*/

SELECT movie.title

FROM actor JOIN casting

ON (actor.id=casting.actorid)

JOIN movie

ON (movie.id = casting.movieid)

WHERE actor.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]*/

SELECT movie.title

FROM actor JOIN casting

ON (actor.id=casting.actorid)

JOIN movie

ON (movie.id = casting.movieid)

WHERE actor.name = 'Harrison Ford' AND casting.ord != 1;

--#10/*List the films together with the leading star for all 1962 films.*/

SELECT movie.title,actor.name

FROM actor JOIN casting

ON (actor.id=casting.actorid)

JOIN movie

ON (movie.id = casting.movieid)

WHERE movie.yr = 1962

AND casting.ord = 1;

知識點:MAX() 函數

MAX 函數返回一列中的最大值。NULL 值不包括在計算中。

MAX() 語法

SELECT MAX(column_name) FROM table_name

--#11

/*

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

*/

SELECT yr,COUNT(title) FROM

movie JOIN casting ON movie.id=movieid

JOIN actor ON actorid=actor.id

where name='John Travolta'

GROUP BY yr

HAVING COUNT(title)=(SELECT MAX(c) FROM

(SELECT yr,COUNT(title) AS c FROM

movie JOIN casting ON movie.id=movieid

JOIN actor ON actorid=actor.id

where name='John Travolta'

GROUP BY yr) AS t

);

--#12

/*

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

*/

SELECT DISTINCT m.title,a.name

FROM (SELECT movie.*

FROM actor JOIN casting

ON (actor.id=casting.actorid)

JOIN movie

ON (movie.id = casting.movieid)

WHERE actor.name = 'Julie Andrews') AS m

JOIN (SELECT actor.*,casting.movieid AS movieid

FROM actor JOIN casting

ON (actor.id=casting.actorid)

WHERE casting.ord = 1) AS a

ON m.id = a.movieid

ORDER BY m.title;

--#13

/*

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

*/

SELECT actor.name

FROM actor

JOIN casting

on (actor.id = casting.actorid)

WHERE casting.ord = 1

GROUP BY actor.name

HAVING COUNT(*) >= 30;

--#14

/*

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

*/

SELECT movie.title, COUNT(actorid) AS cast_actors

FROM movie

JOIN casting

ON movie.id = casting.movieid

JOIN actor

ON actor.id = casting.actorid

WHERE movie.yr = 1978

GROUP BY movie.title

ORDER BY cast_actors DESC,movie.title;

--#15

/*

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

*/

SELECT a.name

FROM (SELECT movie.*

FROM movie

JOIN casting

ON casting.movieid = movie.id

JOIN actor

ON actor.id = casting.actorid

WHERE actor.name = 'Art Garfunkel') AS m

JOIN (SELECT actor.*, casting.movieid

FROM actor

JOIN casting

ON casting.actorid = actor.id

WHERE actor.name != 'Art Garfunkel') as a

ON m.id = a.movieid;

總結

以上是生活随笔為你收集整理的mysql begin operations_MySQL入门(七):More JOIN operations的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。