MySQL获取所有分类的前N条记录

SELECT A1.* 
FROM stock_market AS A1 
   INNER JOIN (SELECT A.code,A.date 
         FROM stock_market AS A 
           LEFT JOIN stock_market AS B 
            ON A.code = B.code 
              AND A.date <= B.date 
         GROUP BY A.code,A.date 
         HAVING COUNT(B.date) <= 370 
  ) AS B1 
  ON A1.code = B1.code 
    AND A1.date = B1.date 
ORDER BY A1.code,A1.date DESC
select a1.* from stock_market a1
inner join
(select a.code,a.date from stock_market a left join stock_market b
on a.code=b.code and a.date<=b.date
group by a.code,a.date
having count(b.date)<=2
)b1
on a1.code=b1.code and a1.date=b1.date
order by a1.code,a1.date desc

 

select a.* from stock_market a where 2 > (select count(*) from stock_market where code = a.code and date> a.date) order by a.code,a.date

 

select a.* from stock_market a where exists (select count(*) from stock_market where code = a.code and date > a.date having Count(*) < 2) order by a.code

 

posted @ 2021-11-27 14:58  锐洋智能  阅读(76)  评论(0编辑  收藏  举报