I want to do a select request that perform a first select and then use that selection to perform a second select.
I made a 1st version using a temp table but I would like to know if there is a way to do it without the temporary table
my code with the temp table is like :
select dvd_name, book_name , count(*) nb
into #t
from usr
inner join book on usr_book_id = book_id
inner join dvd on dvd_id = usr_dvd_id
group by dvd_name, book_name
having count(*) > 1
select top 10 usr_smthg, #t.book_name,dvd_name
from #t
inner join book b on b.book_name = #t.book_name
inner join usr on usr_book_id = book_id
4条答案
按热度按时间wbgh16ku1#
You can use CTE for that
pdsfdshx2#
In sql you can use a sub-query, like this:
roejwanj3#
You can use window function with subquery :
??
indicates ordering column based on you wanttop (10)
records.xzv2uavs4#
Usually, there are better ways to get the proper result, than selecting from a
SELECT
result (such asJOIN
,GROUP BY
, and subselects). However, when using a window function such asROW_NUMBER()
it may become necessary to select from a result.This can be accomplished by
WITH ... AS
.For details see https://www.mysqltutorial.org/mysql-window-functions/mysql-row_number-function/