MySQL-返回每个GROUP BY的X号

MySQL-返回每个GROUP BY的X号,第1张

MySQL-返回每个GROUP BY的X号

尝试像下面那样为每个组生成行号,然后将行数限制为im以下,以显示每组2个结果。

   Select    `marka_name`, `model_name`, `kuzov`, `mileage`, `year`, `rate`from    (    SELECt     @row_num := IF(@prev_value=main.Model_Name,@row_num+1,1) AS RowNumber,     main.*,    @prev_value := main.Model_Name FROM     main,      (SELECT @row_num := 1) x,          (SELECT @prev_value := '') y WHERe     (         (marka_name = 'SUBARU' AND model_name = 'IMPREZA' AND (kuzov = 'GC8' OR kuzov = 'GF8')) OR         (marka_name = 'MAZDA' AND model_name = 'RX-7' AND kuzov = 'FD3S') OR         (marka_name = 'MITSUBISHI' AND model_name = 'LANCER' AND (kuzov = 'CN9A' OR kuzov = 'CP9A')) OR         (marka_name = 'NISSAN' AND model_name = 'SKYLINE' AND (kuzov = 'ER34' OR kuzov = 'BCNR33')) OR         (marka_name = 'NISSAN' AND model_name = 'SILVIA' AND kuzov = 'S14') OR         (marka_name = 'TOYOTA' AND model_name = 'CELICA' AND kuzov = 'ST205') OR         (marka_name = 'TOYOTA' AND model_name = 'ARISTO' AND kuzov = 'JZS161') OR         (marka_name = 'MITSUBISHI' AND model_name = 'DELICA' AND (kuzov = 'PE8W' OR kuzov = 'PD8W' OR kuzov = 'PF8W'))     )     AND     (rate != 'RA' AND rate != 'RR' AND rate != 'A1' AND rate != 'A' AND rate != 'R' AND rate >= '3')     AND     (mileage >= 0 AND mileage <= 150000)     AND     (year >= 1990 AND year <= 1998) ORDER BY     model_name,     mileage ASC,     rate DESC) A where A.RowNumber<=2

要获得每组2条以上的记录,只需更改最后一个子句的位置(如果您每组要获得10条结果)。写在A.RowNumber <= 10的地方

SQL小提琴演示



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5649755.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存