목차
Preface
Presentation
Question Details
Answer
Initialization:
Insert Values
Solution 8
Reference
데이터 베이스 MySQL 튜토리얼 数据库题目整理及详解英文版(五)

数据库题目整理及详解英文版(五)

Jun 07, 2016 pm 02:50 PM
데이터 베이스 정돈하다 영문판 상해

Preface when there is rain neither sun nor moon, the people actually do not think so. Perhaps there are seasonal climate not to be cold rain, let the sun or the cool side. Has the rain the night is otherwise a moonlit night no flavor. Some


Preface

when there is rain neither sun nor moon, the people actually do not think so.

Perhaps there are seasonal climate not to be cold rain, let the sun or the cool side.

Has the rain the night is otherwise a moonlit night no flavor.

Sometimes not reminiscent of Li Shangyin “when he cut a total of west window candle, but then when the famous hope of reunion among friends”

In the rain, fom the sky come the silk notes; here, quiet and comfortable.

in rain


Presentation

In this page, we will show the homework of the course of the Databases Technology in CMU. All of the questions we tested in the postgresql.

This section mainly is to practice the query operation of the SQL, include SQL view, index, etc. We also sure that all the sqls will pass in the mysql, sqlite… , this article also will provide the Chinese version in detail.

中文版


Question Details

In this homework you will have to write SQL queries to answer questions on a movie dataset, about movies and actors. The database contains two tables:

? movies(mid, title, year, num ratings, rating) , Primary key: mid
? play in(mid, name, cast position), Primary key: mid, name

The tables contain the obvious information: which actor played in what movie, at what position; for each movie, we have the title (eg., ’Gone with the wind’), year of production, count of rating reviews it received, and the average score of those ratings (a float in the range 0 to 10, with ’10’ meaning ’excellent’).


We will use Postgres, which is installed in the your own machines.

Question 1: Warm-up queries … … … … … … … … . [5 points]
(a) [2 points] Print all actors in the movie Quantum of Solace, sorted by cast position. Print only their names.
(b) [3 points] Print all movie titles that were released in 2002, with rating larger than 8 and with more than one rating (num ratings > 1).


Question 2: Find the star’s movies … … … … … … . . [5 points]
(a) [5 points] Print movie titles where Sean Connery was the star (i.e. he had position 1 in the cast). Sort the movie titles alphabetically.


Question 3: Popular actors … … … … … … … … . . [15 points]
(a) [8 points] We want to find the actors of the highest quality. We define their quality as the weighted average of the ratings of the movies they have played in (regardless of cast position), using the number of ratings for each movie as the
weight. In other words, we define the quality for a particular actor as

计算公式

Print the names of the top 5 actors, according to the above metric. Break ties alphabetically.

(b) [7 points] Now we want to find the 5 most popular actors, in terms of number of ratings (regardless of positive or negative popularity). I.e, if actor ‘Smith’ played in 2 movies, with num ratings 10 and 15, then Smith’s popularity is 25 (=10+15). Print the top 5 actor names according to popularity. Again, break ties alphabetically.


Question 4: Most controversial actor … … … … … . [10 points]
(a) [10 points] We want to find the most controversial actor. As a measure of controversy, we define the maximum difference between the ratings of two movies that an actor has played in (regardless of cast position). That is, if actor ‘Smith’ played in a movie that got rating=1.2, and another that got rating=9.5, and all the other movies he played in, obtained scores within that range, then Smith’s contoversy score is 9.5-1.2= 8.3. Print the name of the top-most controversial actor - again, if there is a tie in first place, break it alphabetically.


Question 5: The minions … … … … … … … … … . [20 points]
(a) [20 points] Find the “minions” of Annette Nicole: Print the names of actors who only played in movies with her and never without her. The answer should not contain the name of Annette Nicole. Order the names alphabetically.


Question 6: High productivity … … … … … … … … [5 points]
(a) [5 points] Find the top 2 most productive years (by number of movies produced). Solve ties by preferring chronologically older years, and print only the years.


Question 7: Movies with similar cast … … … … … . [15 points]
(a) [8 points] Print the count of distinct pairs of movies that have at least one actor in common (ignoring cast position). Exclude self-pairs, and mirror-pairs.
(b) [7 points] Print the count of distinct pairs of moves that have at least two actors in common (again, ignoring cast position). Again, exclude self-pairs, and mirror pairs.


Question 8: Skyline query … … … … … … … … … [25 points]
(a) [25 points] We want to find a set of movies that have both high popularity (ie, high num ratings) as well as high quality (rating). No single movie may achieve both - in which case, we want the so-called Skyline query 2 . More specifically, we want all movies that are not “dominated” by any other movie:

Definition of domination : Movie “A” dominates movie “B” if movie “A” wins over movie “B”, on both criteria, or wins on one, and ties on the rest.

Figure 1 gives a pictorial example: the solid dots (’A’, ’D’, ’F’) are not dominated by any other dot, and thus form the skyline. All other dots are dominated by at least one other dot: e.g., dot ’B’ is dominated by dot ’A’, being inside the shaded rectangle that has ’A’ as the upper-right corner.

figure 1

Figure 1: Illustration of Skyline and domination : ’A’ dominates all points in the shaded rectangle; ’A’, ’D’ and ’F’ form the skyline of this cloud of points.

Given the above description, print the title of all the movies on the skyline, along with the rating and the number of ratings.


Answer

we give the Postgres version in detail, we will see you can tranfer it easily in mysql or sqlite.

Initialization:

<code class=" hljs sql">## <span class="hljs-operator"><span class="hljs-keyword">drop</span> the <span class="hljs-keyword">table</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">exists</span>
<span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">exists</span> movies <span class="hljs-keyword">cascade</span>;</span>   
<span class="hljs-operator"><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">exists</span> play_in <span class="hljs-keyword">cascade</span>;</span>

## <span class="hljs-operator"><span class="hljs-keyword">create</span> tables movies <span class="hljs-keyword">and</span> play_in
<span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> movies (
mid <span class="hljs-keyword">integer</span> <span class="hljs-keyword">PRIMARY</span> <span class="hljs-keyword">KEY</span>, 
title <span class="hljs-keyword">varchar</span>(<span class="hljs-number">200</span>),
<span class="hljs-keyword">year</span> <span class="hljs-keyword">integer</span>,
num_ratings <span class="hljs-keyword">integer</span>, 
rating <span class="hljs-keyword">real</span>);</span>

<span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> play_in (
mid <span class="hljs-keyword">integer</span> <span class="hljs-keyword">references</span> movies, 
name <span class="hljs-keyword">varchar</span>(<span class="hljs-number">100</span>), 
cast_position <span class="hljs-keyword">integer</span>, 
<span class="hljs-keyword">PRIMARY</span> <span class="hljs-keyword">KEY</span>(mid, name));</span>

<span class="hljs-operator"><span class="hljs-keyword">create</span> index mid <span class="hljs-keyword">on</span> movies(mid);</span></code>
로그인 후 복사

Insert Values

Insert into some values into the table movies and play_in,
you will find the datas just in the follow links in my 360 yunFiles:

https://yunpan.cn/cSfLzxQApRXSi password: f3ab

<code class=" hljs rust">## <span class="hljs-keyword">use</span> <span class="hljs-string">"copy"</span> in Postgres
\<span class="hljs-keyword">copy</span> movies from <span class="hljs-string">'~/data/movie_processed.dat'</span>;
\<span class="hljs-keyword">copy</span> play_in from <span class="hljs-string">'~/data/movie_actor_processed.dat'</span>;

## <span class="hljs-keyword">if</span> you <span class="hljs-keyword">use</span> other databases(mysql, sqlite), you can <span class="hljs-keyword">use</span>   the sql statement: <span class="hljs-string">"insert into ... valuse()"</span>
</code>
로그인 후 복사

The flowing image show the test infos in my ubuntu os:

ctreate and copy datas1

ctreate and copy datas2


Solution 1

<code class=" hljs asciidoc">(a) SELECT name FROM play<span class="hljs-emphasis">_in p, movies m
WHERE p.mid = m.mid and m.title=’Quantum of Solace’
ORDER BY p.cast_</span>position;

## (a) Result just like this:

<span class="hljs-header">name
------------------------------</span>
Daniel Craig
Olga Kurylenko
Mathieu Amalric
Judi Dench
Giancarlo Giannini
Gemma Arterton
Jeffrey Wright
David Harbour
Jesper Christensen
Anatole Taubman
Rory Kinnear
Tim Pigott-Smith
Fernando Guillen-Cuervo
Jesus Ochoa
Glenn Foster
Paul Ritter
Simon Kassianides
Stana Katic
Lucrezia Lante della Rove...
Neil Jackson
Oona Chaplin
(21 rows)

(b) SELECT title FROM movies
WHERE year = 2002 and rating>8 and num<span class="hljs-emphasis">_ratings>1;

</span>## (b) Result just like this:
<span class="hljs-header">title
---------------------------------------</span>
The Lord of the Rings: The Two Towers
Cidade de Deus
Mou gaan dou
(3 rows)
</code>
로그인 후 복사

The flowing image show the test solution 1 infos in my ubuntu os:

3

3.1

4


Solution 2

<code class=" hljs vbnet"><span class="hljs-keyword">SELECT</span> title <span class="hljs-keyword">from</span> movies m, play_in p
<span class="hljs-keyword">WHERE</span> m.<span class="hljs-keyword">mid</span> = p.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span> 
name = ’Sean Connery’ <span class="hljs-keyword">and</span> 
cast_position = <span class="hljs-number">1</span>
<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> title;

<span class="hljs-preprocessor">## Result just like this:</span>
title
---------------------------------------
Der Name der Rose
Diamonds Are Forever
Dr. No
Entrapment
Finding Forrester
First Knight
<span class="hljs-keyword">From</span> Russia <span class="hljs-keyword">with</span> Love
Goldfinger
Never Say Never Again
The Hunt <span class="hljs-keyword">for</span> Red October
The League <span class="hljs-keyword">of</span> Extraordinary Gentlemen
Thunderball
You Only Live Twice
(<span class="hljs-number">13</span> rows)</code>
로그인 후 복사

The flowing image show the test solution 2 infos in my ubuntu os:

5


Solution 3

<code class=" hljs sql">(a) <span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> WeigthedRatings;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> WeightedRatings <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> name, <span class="hljs-aggregate">SUM</span>(rating*num_ratings)/<span class="hljs-aggregate">SUM</span>(num_ratings) <span class="hljs-keyword">AS</span> WeightedRating
<span class="hljs-keyword">FROM</span> movies m, play_in p <span class="hljs-keyword">WHERE</span> m.mid = p.mid <span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span>(name);</span>

<span class="hljs-operator"><span class="hljs-keyword">SELECT</span> name <span class="hljs-keyword">FROM</span> WeightedRatings
<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span>
WeightedRating <span class="hljs-keyword">DESC</span>, name <span class="hljs-keyword">ASC</span> LIMIT <span class="hljs-number">5</span>;</span>

## (a) Result just like this:
name
<span class="hljs-comment">-----------------------</span>
Adam Kalesperis
Aidan Feore
Aleksandr Kajdanovsky
Alexander Kaidanovsky
Alisa Frejndlikh
(5 rows)

(b) <span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> ActorSumRatings;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> ActorSumRatings <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> name, <span class="hljs-aggregate">SUM</span>(num_ratings) <span class="hljs-keyword">as</span> popularity
<span class="hljs-keyword">FROM</span> play_in p, movies m
<span class="hljs-keyword">WHERE</span> p.mid = m.mid
<span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> name;</span>

<span class="hljs-operator"><span class="hljs-keyword">SELECT</span> name <span class="hljs-keyword">from</span> ActorSumRatings
<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> popularity <span class="hljs-keyword">DESC</span>, name <span class="hljs-keyword">ASC</span> LIMIT <span class="hljs-number">5</span>;</span>

## (b) Result just like this:
name
<span class="hljs-comment">----------------------</span>
Johnny Depp
Alan Rickman
Orlando Bloom
Helena Bonham Carter
Matt Damon
(5 rows)</code>
로그인 후 복사

The flowing images show the test solution 3 infos in my ubuntu os:

6

7


Solution 4

<code class=" hljs sql"><span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> RatingGap;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> RatingGap <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> p1.name, <span class="hljs-aggregate">MAX</span>(ABS(m1.rating-m2.rating)) <span class="hljs-keyword">as</span> Gap
<span class="hljs-keyword">FROM</span> play_in p1, play_in p2, movies m1, movies m2
<span class="hljs-keyword">WHERE</span> p1.mid = m1.mid <span class="hljs-keyword">and</span> 
p2.mid = m2.mid <span class="hljs-keyword">and</span> 
p1.name = p2.name
<span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span>(p1.name);</span>

<span class="hljs-operator"><span class="hljs-keyword">SELECT</span> name 
<span class="hljs-keyword">FROM</span> RatingGap 
<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span>(Gap) <span class="hljs-keyword">DESC</span> LIMIT <span class="hljs-number">1</span>;</span>

## Result just like this:
name
<span class="hljs-comment">---------------</span>
John Travolta
(1 row)</code>
로그인 후 복사

The flowing image show the test solution 4 infos in my ubuntu os:

8.1

8.3


Solution 5

<code class=" hljs sql"><span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> MastersMovies <span class="hljs-keyword">CASCADE</span>;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> MastersMovies <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> m.mid,m.title <span class="hljs-keyword">FROM</span> movies m, play_in p
<span class="hljs-keyword">WHERE</span> m.mid = p.mid <span class="hljs-keyword">and</span> p.name = ’Annette Nicole’;</span>

<span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> CoActors;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> CoActors <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">DISTINCT</span> name <span class="hljs-keyword">FROM</span> MastersMovies m , play_in p
<span class="hljs-keyword">WHERE</span> p.mid = m.mid;</span>

<span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> Combinations;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> Combinations <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> name,mid <span class="hljs-keyword">FROM</span> MastersMovies , CoActors;</span>

<span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> NonExistent;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> NonExistent <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> * <span class="hljs-keyword">FROM</span> Combinations
<span class="hljs-keyword">EXCEPT</span> (<span class="hljs-keyword">SELECT</span> name, mid <span class="hljs-keyword">FROM</span> play_in);</span>

<span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> PotentialResults;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> PotentialResults <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> * <span class="hljs-keyword">from</span> CoActors
<span class="hljs-keyword">EXCEPT</span> (<span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">distinct</span>(name) <span class="hljs-keyword">FROM</span> NonExistent);</span>

<span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> NotMastersMovies;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> NotMastersMovies <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> m.mid <span class="hljs-keyword">FROM</span> movies m
<span class="hljs-keyword">EXCEPT</span> (<span class="hljs-keyword">SELECT</span> mid <span class="hljs-keyword">FROM</span> MastersMovies);</span>

<span class="hljs-operator"><span class="hljs-keyword">SELECT</span> * <span class="hljs-keyword">from</span> PotentialResults
<span class="hljs-keyword">WHERE</span> name <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> 
(<span class="hljs-keyword">SELECT</span> name
<span class="hljs-keyword">FROM</span> play_in p, NotMastersMovies m
<span class="hljs-keyword">WHERE</span> m.mid = p.mid
<span class="hljs-keyword">UNION</span> <span class="hljs-keyword">SELECT</span> ’Annette Nicole’
) <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> name;</span>

## Result just like this:
name
<span class="hljs-comment">-----------------</span>
Christian Perry
(1 row)
</code>
로그인 후 복사

The flowing image show the test solution 5 infos in my ubuntu os:

8.2


Solution 6

<code class=" hljs sql"><span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> MoviesPerYear;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> MoviesPerYear <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">year</span>, <span class="hljs-aggregate">COUNT</span>(title) num_movies
<span class="hljs-keyword">FROM</span> MOVIES <span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span>(<span class="hljs-keyword">year</span>);</span>

<span class="hljs-operator"><span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">year</span> <span class="hljs-keyword">from</span> MoviesPerYear 
<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> num_movies <span class="hljs-keyword">DESC</span> LIMIT <span class="hljs-number">2</span>;</span>

## Result just like this:
year
<span class="hljs-comment">------</span>
2006
2007
(2 rows)</code>
로그인 후 복사

The flowing image show the test solution 6 infos in my ubuntu os:

9


Solution 7

<code class=" hljs vbnet">(a) <span class="hljs-keyword">SELECT</span> COUNT(*) <span class="hljs-keyword">FROM</span> 
(<span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">DISTINCT</span> m1.<span class="hljs-keyword">mid</span>, m2.<span class="hljs-keyword">mid</span>
 <span class="hljs-keyword">FROM</span> movies m1, movies m2, play_in p1, play_in p2
 <span class="hljs-keyword">WHERE</span> m1.<span class="hljs-keyword">mid</span> > m2.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span>
 m1.<span class="hljs-keyword">mid</span> = p1.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span>
 m2.<span class="hljs-keyword">mid</span> = p2.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span> 
 p1.name = p2.name) 
<span class="hljs-keyword">AS</span> count;

<span class="hljs-preprocessor">## (a) Result just like this:</span>
count
--------
<span class="hljs-number">104846</span>
(<span class="hljs-number">1</span> row)

(b) <span class="hljs-keyword">SELECT</span> COUNT(*) <span class="hljs-keyword">FROM</span> 
(<span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">DISTINCT</span> m1.<span class="hljs-keyword">mid</span>, m2.<span class="hljs-keyword">mid</span>
<span class="hljs-keyword">FROM</span> movies m1, movies m2, play_in p1,
play_in p2, play_in p3, play_in p4
<span class="hljs-keyword">WHERE</span> m1.<span class="hljs-keyword">mid</span> > m2.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span> 
m1.<span class="hljs-keyword">mid</span> = p1.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span> 
m2.<span class="hljs-keyword">mid</span> = p2.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span>
m1.<span class="hljs-keyword">mid</span> = p3.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span> 
m2.<span class="hljs-keyword">mid</span> = p4.<span class="hljs-keyword">mid</span> <span class="hljs-keyword">and</span> 
p2.name <> p4.name <span class="hljs-keyword">and</span> 
p1.name = p2.name <span class="hljs-keyword">and</span> 
p3.name = p4.name) 
<span class="hljs-keyword">AS</span> count;

<span class="hljs-preprocessor">## (b) Result just like this:</span>
count
-------
<span class="hljs-number">6845</span>
(<span class="hljs-number">1</span> row)</code>
로그인 후 복사

The flowing image show the test solution 7 infos in my ubuntu os:

11


Solution 8

<code class=" hljs sql"><span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> Dominated;</span>

<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> Dominated <span class="hljs-keyword">AS</span>
<span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">DISTINCT</span> m2.mid, m2.title,m2.num_ratings, m2.rating
<span class="hljs-keyword">FROM</span> movies m1, movies m2
<span class="hljs-keyword">WHERE</span> m2.rating<=m1.rating <span class="hljs-keyword">and</span> m2.num_ratings<=m1.num_ratings <span class="hljs-keyword">and</span> 
<span class="hljs-keyword">NOT</span> (m2.rating = m1.rating <span class="hljs-keyword">and</span> m2.num_ratings=m1.num_ratings);</span>

<span class="hljs-operator"><span class="hljs-keyword">SELECT</span> title,num_ratings,rating 
<span class="hljs-keyword">FROM</span> movies
<span class="hljs-keyword">EXCEPT</span> (<span class="hljs-keyword">SELECT</span> title,num_ratings,rating <span class="hljs-keyword">FROM</span> Dominated);</span>
</code>
로그인 후 복사

The flowing image show the test solution 8 infos in my ubuntu os:

12


Reference

[1] http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html
[2] http://www.postgresql.org/docs/
[3] http://www.cs.cmu.edu/~epapalex/15415S14/PostgreSQLReadme.htm
[4] http://www.cs.cmu.edu/~christos/courses/

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Go 언어는 데이터베이스의 추가, 삭제, 수정 및 쿼리 작업을 어떻게 구현합니까? Go 언어는 데이터베이스의 추가, 삭제, 수정 및 쿼리 작업을 어떻게 구현합니까? Mar 27, 2024 pm 09:39 PM

Go 언어는 효율적이고 간결하며 배우기 쉬운 프로그래밍 언어입니다. 동시 프로그래밍과 네트워크 프로그래밍의 장점 때문에 개발자들이 선호합니다. 실제 개발에서 데이터베이스 작업은 필수적인 부분입니다. 이 기사에서는 Go 언어를 사용하여 데이터베이스 추가, 삭제, 수정 및 쿼리 작업을 구현하는 방법을 소개합니다. Go 언어에서는 일반적으로 사용되는 SQL 패키지, Gorm 등과 같은 타사 라이브러리를 사용하여 데이터베이스를 운영합니다. 여기서는 sql 패키지를 예로 들어 데이터베이스의 추가, 삭제, 수정 및 쿼리 작업을 구현하는 방법을 소개합니다. MySQL 데이터베이스를 사용하고 있다고 가정합니다.

Hibernate는 어떻게 다형성 매핑을 구현합니까? Hibernate는 어떻게 다형성 매핑을 구현합니까? Apr 17, 2024 pm 12:09 PM

Hibernate 다형성 매핑은 상속된 클래스를 데이터베이스에 매핑할 수 있으며 다음 매핑 유형을 제공합니다. Join-subclass: 상위 클래스의 모든 열을 포함하여 하위 클래스에 대한 별도의 테이블을 생성합니다. 클래스별 테이블: 하위 클래스별 열만 포함하는 하위 클래스에 대한 별도의 테이블을 만듭니다. Union-subclass: Joined-subclass와 유사하지만 상위 클래스 테이블이 모든 하위 클래스 열을 통합합니다.

iOS 18에는 손실되거나 손상된 사진을 검색할 수 있는 새로운 '복구된' 앨범 기능이 추가되었습니다. iOS 18에는 손실되거나 손상된 사진을 검색할 수 있는 새로운 '복구된' 앨범 기능이 추가되었습니다. Jul 18, 2024 am 05:48 AM

Apple의 최신 iOS18, iPadOS18 및 macOS Sequoia 시스템 릴리스에는 사진 애플리케이션에 중요한 기능이 추가되었습니다. 이 기능은 사용자가 다양한 이유로 손실되거나 손상된 사진과 비디오를 쉽게 복구할 수 있도록 설계되었습니다. 새로운 기능에는 사진 앱의 도구 섹션에 '복구됨'이라는 앨범이 도입되었습니다. 이 앨범은 사용자가 기기에 사진 라이브러리에 포함되지 않은 사진이나 비디오를 가지고 있을 때 자동으로 나타납니다. "복구된" 앨범의 출현은 데이터베이스 손상으로 인해 손실된 사진과 비디오, 사진 라이브러리에 올바르게 저장되지 않은 카메라 응용 프로그램 또는 사진 라이브러리를 관리하는 타사 응용 프로그램에 대한 솔루션을 제공합니다. 사용자는 몇 가지 간단한 단계만 거치면 됩니다.

HTML이 데이터베이스를 읽는 방법에 대한 심층 분석 HTML이 데이터베이스를 읽는 방법에 대한 심층 분석 Apr 09, 2024 pm 12:36 PM

HTML은 데이터베이스를 직접 읽을 수 없지만 JavaScript 및 AJAX를 통해 읽을 수 있습니다. 단계에는 데이터베이스 연결 설정, 쿼리 보내기, 응답 처리 및 페이지 업데이트가 포함됩니다. 이 기사에서는 JavaScript, AJAX 및 PHP를 사용하여 MySQL 데이터베이스에서 데이터를 읽는 실제 예제를 제공하고 쿼리 결과를 HTML 페이지에 동적으로 표시하는 방법을 보여줍니다. 이 예제에서는 XMLHttpRequest를 사용하여 데이터베이스 연결을 설정하고 쿼리를 보내고 응답을 처리함으로써 페이지 요소에 데이터를 채우고 데이터베이스를 읽는 HTML 기능을 실현합니다.

PHP에서 MySQLi를 사용하여 데이터베이스 연결을 설정하는 방법에 대한 자세한 튜토리얼 PHP에서 MySQLi를 사용하여 데이터베이스 연결을 설정하는 방법에 대한 자세한 튜토리얼 Jun 04, 2024 pm 01:42 PM

MySQLi를 사용하여 PHP에서 데이터베이스 연결을 설정하는 방법: MySQLi 확장 포함(require_once) 연결 함수 생성(functionconnect_to_db) 연결 함수 호출($conn=connect_to_db()) 쿼리 실행($result=$conn->query()) 닫기 연결( $conn->close())

PHP에서 데이터베이스 연결 오류를 처리하는 방법 PHP에서 데이터베이스 연결 오류를 처리하는 방법 Jun 05, 2024 pm 02:16 PM

PHP에서 데이터베이스 연결 오류를 처리하려면 다음 단계를 사용할 수 있습니다. mysqli_connect_errno()를 사용하여 오류 코드를 얻습니다. 오류 메시지를 얻으려면 mysqli_connect_error()를 사용하십시오. 이러한 오류 메시지를 캡처하고 기록하면 데이터베이스 연결 문제를 쉽게 식별하고 해결할 수 있어 애플리케이션이 원활하게 실행될 수 있습니다.

PHP를 사용하여 데이터베이스에서 중국어 왜곡 문자를 처리하기 위한 팁과 사례 PHP를 사용하여 데이터베이스에서 중국어 왜곡 문자를 처리하기 위한 팁과 사례 Mar 27, 2024 pm 05:21 PM

PHP는 웹사이트 개발에 널리 사용되는 백엔드 프로그래밍 언어로, 강력한 데이터베이스 운영 기능을 갖추고 있으며 MySQL과 같은 데이터베이스와 상호 작용하는 데 자주 사용됩니다. 그러나 한자 인코딩의 복잡성으로 인해 데이터베이스에서 잘못된 한자를 처리할 때 문제가 자주 발생합니다. 이 기사에서는 잘못된 문자의 일반적인 원인, 솔루션 및 특정 코드 예제를 포함하여 데이터베이스에서 중국어 잘못된 문자를 처리하기 위한 PHP의 기술과 사례를 소개합니다. 문자가 왜곡되는 일반적인 이유는 잘못된 데이터베이스 문자 집합 설정 때문입니다. 데이터베이스를 생성할 때 utf8 또는 u와 같은 올바른 문자 집합을 선택해야 합니다.

Golang에서 데이터베이스 콜백 함수를 사용하는 방법은 무엇입니까? Golang에서 데이터베이스 콜백 함수를 사용하는 방법은 무엇입니까? Jun 03, 2024 pm 02:20 PM

Golang의 데이터베이스 콜백 기능을 사용하면 다음을 달성할 수 있습니다. 지정된 데이터베이스 작업이 완료된 후 사용자 정의 코드를 실행합니다. 추가 코드를 작성하지 않고도 별도의 함수를 통해 사용자 정의 동작을 추가할 수 있습니다. 삽입, 업데이트, 삭제, 쿼리 작업에 콜백 함수를 사용할 수 있습니다. 콜백 함수를 사용하려면 sql.Exec, sql.QueryRow, sql.Query 함수를 사용해야 합니다.

See all articles