+----+---------+
| id | content |
+----+---------+
|  8 | 8       |
| 14 | 8       |
| 17 | 8       |
|  9 | 7       |
| 15 | 7       |
| 16 | 7       |
| 11 | 6       |
| 12 | 6       |
| 13 | 6       |
| 18 | 6       |
| 19 | 6       |
+----+---------+
就是content相同的数据分别取3条,比如上面(content=8,7,6的分别取3条就足够了。)
结果:
+----+---------+
| id | content |
+----+---------+
|  8 | 8       |
| 14 | 8       |
| 17 | 8       |
|  9 | 7       |
| 15 | 7       |
| 16 | 7       |
| 11 | 6       |
| 12 | 6       |
| 13 | 6       |
+----+---------+
求助,非常感谢!sql弱势,记得在sql server中用row_number实现过,可是 刚用mysql不是很熟悉,万分感谢MySQLSQL

解决方案 »

  1.   


    if OBJECT_ID('tempdb.dbo.#Content') is not null drop table #Content
    create table #Content
    (
    id int,
    content int
    )insert into #Content(id,content)values
    ('8','8'),('14','8'),('17','8'),('9','7'),('15','7'),
    ('16','7'),('11','6'),('12','6'),('13','6'),('18','6'),('19','6')select 
    id,content
    from
    (
    select 
    ROW_NUMBER() over (PARTITION by content order by id) row,
    id,content
    from #Content
    ) a
    where a.row<=3
    order by content,idif OBJECT_ID('tempdb.dbo.#Content') is not null drop table #Content
      

  2.   

    select *
    from tb A
    where 3>(select count(*) from tb content=A.content and A.id>id)
      

  3.   

    SELECT * FROM AA A1 WHERE 3>(SELECT COUNT(*) FROM AA WHERE A1.content =content  AND 
    A1.ID>ID)
      

  4.   

    参考下贴中的多种方法http://blog.csdn.net/acmain_chm/article/details/4126306
    [征集]分组取最大N条记录方法征集,及散分....
      

  5.   

    MySQL中处理这种问题,典型的可以使用变量,排序、编号,实现row_number功能,再筛选。select id,content 
    from 
    (
    select t1.id,
           t1.content,
           case when @vcontent=t1.content then @num:=@num+1
                else @num:=1
           end as num,
           @vcontent:=t1.content
    from t0530 t1,
     (select @vcontent:=0,@num:=0) t2
    order by t1.content
    ) T
    where T.num <=3扩展阅读
      

  6.   

    这一类的功能太常见了,可以看看CSDN大牛邹建的书籍,不用单独拿出来问
      

  7.   

    http://my.csdn.net/zjcxc