mysql - 如何获取mysql中每个组的前300行?

标签 mysql

我有这样的 table

dlrecipientid  dlrecipientlistid
1                  1
2                  1
..                 ..
999                1
1000               1
1001               2
1002               2
..                 ..
1999               2
2000               2

如何查询并获取前300行dlrecipientlistid

示例

dlrecipientid  dlrecipientlistid
1                  1
2                  1
..                 ..
299                1
300                1
1001               2
1002               2
..                 ..
1299               2
1300               2

我需要在mysql中查询

最佳答案

您可以通过以下方式进行自加入:

  SELECT tbl1.* FROM your_table AS tbl1
  LEFT OUTER JOIN your_table AS tbl2 
    ON (tbl1.dlrecipientlistid= tbl2.dlrecipientlistid 
        AND tbl1.dlrecipientid > tbl2.dlrecipientid)
  GROUP BY tbl1.dlrecipientid  
  HAVING COUNT(*) < 300
  ORDER BY tbl1.dlrecipientid,tbl1.dlrecipientlistid DESC;

我已经更新了查询并在 SQLFIDDLE 中进行了充分测试

关于mysql - 如何获取mysql中每个组的前300行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42755992/

相关文章:

java - 将Java连接到MySQL数据库

php - 在cakephp中更新多表

mysql - 从mysql中的xml中提取值

mysql - 显示格式为 "Monthname dd-dd, YYYY"的两个日期字段值

C# winforms datagridview 显示 MySql 表

mysql - SQL,查找满足严格条件的订单,匹配项目,如果购买了其他项目则不匹配

mySQL(左连接?)

php - 如何启动 PHP?

mysql - 如何使用 MySQL Workbench 创建 MySQL 脚本?

php - 使用 MySQL 用户通过 php 登录 - 为什么不呢?