Inhaltsverzeichnis
Preface
Presentation
Question Details
Answer
Initialization:
Insert Values
Solution 8
Reference
Heim Datenbank MySQL-Tutorial 数据库题目整理及详解英文版(五)

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

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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/

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Java-Tutorial
1660
14
PHP-Tutorial
1261
29
C#-Tutorial
1234
24
iOS 18 fügt eine neue Albumfunktion „Wiederhergestellt' hinzu, um verlorene oder beschädigte Fotos wiederherzustellen iOS 18 fügt eine neue Albumfunktion „Wiederhergestellt' hinzu, um verlorene oder beschädigte Fotos wiederherzustellen Jul 18, 2024 am 05:48 AM

Apples neueste Versionen der iOS18-, iPadOS18- und macOS Sequoia-Systeme haben der Fotoanwendung eine wichtige Funktion hinzugefügt, die Benutzern dabei helfen soll, aus verschiedenen Gründen verlorene oder beschädigte Fotos und Videos einfach wiederherzustellen. Mit der neuen Funktion wird im Abschnitt „Extras“ der Fotos-App ein Album mit dem Namen „Wiederhergestellt“ eingeführt, das automatisch angezeigt wird, wenn ein Benutzer Bilder oder Videos auf seinem Gerät hat, die nicht Teil seiner Fotobibliothek sind. Das Aufkommen des Albums „Wiederhergestellt“ bietet eine Lösung für Fotos und Videos, die aufgrund einer Datenbankbeschädigung verloren gehen, die Kameraanwendung nicht korrekt in der Fotobibliothek speichert oder eine Drittanbieteranwendung die Fotobibliothek verwaltet. Benutzer benötigen nur wenige einfache Schritte

Wie implementiert Hibernate polymorphe Zuordnung? Wie implementiert Hibernate polymorphe Zuordnung? Apr 17, 2024 pm 12:09 PM

Die polymorphe Hibernate-Zuordnung kann geerbte Klassen der Datenbank zuordnen und bietet die folgenden Zuordnungstypen: Joined-Subclass: Erstellen Sie eine separate Tabelle für die Unterklasse, einschließlich aller Spalten der übergeordneten Klasse. Tabelle pro Klasse: Erstellen Sie eine separate Tabelle für Unterklassen, die nur unterklassenspezifische Spalten enthält. Union-Unterklasse: ähnelt der verbundenen Unterklasse, aber die Tabelle der übergeordneten Klasse vereint alle Spalten der Unterklasse.

Ausführliches Tutorial zum Herstellen einer Datenbankverbindung mit MySQLi in PHP Ausführliches Tutorial zum Herstellen einer Datenbankverbindung mit MySQLi in PHP Jun 04, 2024 pm 01:42 PM

So verwenden Sie MySQLi zum Herstellen einer Datenbankverbindung in PHP: MySQLi-Erweiterung einbinden (require_once) Verbindungsfunktion erstellen (functionconnect_to_db) Verbindungsfunktion aufrufen ($conn=connect_to_db()) Abfrage ausführen ($result=$conn->query()) Schließen Verbindung ( $conn->close())

Umgang mit Datenbankverbindungsfehlern in PHP Umgang mit Datenbankverbindungsfehlern in PHP Jun 05, 2024 pm 02:16 PM

Um Datenbankverbindungsfehler in PHP zu behandeln, können Sie die folgenden Schritte ausführen: Verwenden Sie mysqli_connect_errno(), um den Fehlercode abzurufen. Verwenden Sie mysqli_connect_error(), um die Fehlermeldung abzurufen. Durch die Erfassung und Protokollierung dieser Fehlermeldungen können Datenbankverbindungsprobleme leicht identifiziert und behoben werden, wodurch der reibungslose Betrieb Ihrer Anwendung gewährleistet wird.

Wie verwende ich Datenbank-Callback-Funktionen in Golang? Wie verwende ich Datenbank-Callback-Funktionen in Golang? Jun 03, 2024 pm 02:20 PM

Durch die Verwendung der Datenbank-Rückruffunktion in Golang kann Folgendes erreicht werden: Ausführen von benutzerdefiniertem Code, nachdem der angegebene Datenbankvorgang abgeschlossen ist. Fügen Sie benutzerdefiniertes Verhalten durch separate Funktionen hinzu, ohne zusätzlichen Code zu schreiben. Rückruffunktionen stehen für Einfüge-, Aktualisierungs-, Lösch- und Abfragevorgänge zur Verfügung. Sie müssen die Funktion sql.Exec, sql.QueryRow oder sql.Query verwenden, um die Rückruffunktion verwenden zu können.

Wie verbinde ich mich mit Golang mit einer Remote-Datenbank? Wie verbinde ich mich mit Golang mit einer Remote-Datenbank? Jun 01, 2024 pm 08:31 PM

Über das Datenbank-/SQL-Paket der Go-Standardbibliothek können Sie eine Verbindung zu Remote-Datenbanken wie MySQL, PostgreSQL oder SQLite herstellen: Erstellen Sie eine Verbindungszeichenfolge mit Datenbankverbindungsinformationen. Verwenden Sie die Funktion sql.Open(), um eine Datenbankverbindung zu öffnen. Führen Sie Datenbankoperationen wie SQL-Abfragen und Einfügeoperationen durch. Verwenden Sie „defer“, um die Datenbankverbindung zu schließen und Ressourcen freizugeben.

Wie speichere ich JSON-Daten in einer Datenbank in Golang? Wie speichere ich JSON-Daten in einer Datenbank in Golang? Jun 06, 2024 am 11:24 AM

JSON-Daten können mithilfe der gjson-Bibliothek oder der json.Unmarshal-Funktion in einer MySQL-Datenbank gespeichert werden. Die gjson-Bibliothek bietet praktische Methoden zum Parsen von JSON-Feldern, und die Funktion json.Unmarshal erfordert einen Zieltypzeiger zum Unmarshalieren von JSON-Daten. Bei beiden Methoden müssen SQL-Anweisungen vorbereitet und Einfügevorgänge ausgeführt werden, um die Daten in der Datenbank beizubehalten.

Wie gehe ich mit Datenbankverbindungen und -operationen mit C++ um? Wie gehe ich mit Datenbankverbindungen und -operationen mit C++ um? Jun 01, 2024 pm 07:24 PM

Verwenden Sie die DataAccessObjects (DAO)-Bibliothek in C++, um die Datenbank zu verbinden und zu betreiben, einschließlich der Einrichtung von Datenbankverbindungen, der Ausführung von SQL-Abfragen, dem Einfügen neuer Datensätze und der Aktualisierung vorhandener Datensätze. Die spezifischen Schritte sind: 1. Erforderliche Bibliotheksanweisungen einschließen; 3. Ein Recordset-Objekt erstellen, um SQL-Abfragen auszuführen oder Daten zu bearbeiten; 4. Die Ergebnisse durchlaufen oder Datensätze entsprechend den spezifischen Anforderungen aktualisieren;

See all articles