SELECT title, name FROM movies_stars JOIN stars ON movies_stars.star_id = stars.id JOIN movies ON movies_stars.movie_id = movies.id WHERE name ='Morgan Freeman' OR name ='Tom Hardy' ORDERBY name;
得到以下结果:
1 2 3 4 5 6 7 8 9
┌──────────────────────────┬────────────────┐ │ title │ name │ ├──────────────────────────┼────────────────┤ │ The Shawshank Redemption │ Morgan Freeman │ │ Se7en │ Morgan Freeman │ │ The Dark Knight Rises │ Morgan Freeman │ │ Inception │ Tom Hardy │ │ The Dark Knight Rises │ Tom Hardy │ └──────────────────────────┴────────────────┘
还有一种写法,效果是一样的:
1 2 3 4 5
SELECT title, name FROM movies_stars, stars, movies WHERE movies_stars.star_id = stars.id AND movies_stars.movie_id = movies.id AND name IN ('Morgan Freeman', 'Tom Hardy') ORDERBY name;