如何在没有任何子查询的情况下重写SQL查询?

时间:2022-03-13 15:45:57

How can I rewrite this query without using any subqueries in SQL? I'm not too familiar with how to do this, but I think it's done by using "join."

如何在不使用SQL中的任何子查询的情况下重写此查询?我不太熟悉如何做到这一点,但我认为这是通过使用“加入”来完成的。

SELECT title
FROM Movies Old
Where year < ANY
  (SELECT year
  FROM Movies
  WHERE title = Old. title
  );

(Note: this comes from the relation Movies(title, year, length, genre, studioName, producerC#))

(注意:这来自关系电影(标题,年份,长度,流派,工作室名称,制作者C#))

1 个解决方案

#1


2  

To literally rewrite your current query using joins you can try this:

要使用连接从字面上重写当前查询,您可以尝试这样做:

SELECT m1.title
FROM Movies m1
INNER JOIN Movies m2
    ON m1.title = m2.title AND
       m1.year < m2.year

But if all you really want is to find movie titles which appear more than once, then when not just use a GROUP BY query:

但如果您真正想要的是找到不止一次出现的电影片头,那么当不仅仅使用GROUP BY查询时:

SELECT title
FROM Movies
GROUP BY title
HAVING COUNT(*) > 1

#1


2  

To literally rewrite your current query using joins you can try this:

要使用连接从字面上重写当前查询,您可以尝试这样做:

SELECT m1.title
FROM Movies m1
INNER JOIN Movies m2
    ON m1.title = m2.title AND
       m1.year < m2.year

But if all you really want is to find movie titles which appear more than once, then when not just use a GROUP BY query:

但如果您真正想要的是找到不止一次出现的电影片头,那么当不仅仅使用GROUP BY查询时:

SELECT title
FROM Movies
GROUP BY title
HAVING COUNT(*) > 1