PHP+MYSQL环境!
表是这样的
ID name sorid
1  xx   2
2  xx   2
3  xx   2
4  xx   3
5  xx   3
6  xx   3
7  xx   4
8  xx   4
9  xx   4相同sortid的只取2条,多余的不要,请问这样的sql语句应该怎么写!能实现吗?

解决方案 »

  1.   


    select * from tbl_name t1 where (select count(*) from tbl_name t2 where t2.sortid=t1.sortid and t2.id < t1.id) < 2
    另外,为保证性能,sortid字段应设置为索引。
      

  2.   



      select dinstinct sortid from $table 
    查询出所有的sortid
    得到数组$row
    for($row  as $ele){
       $query="select * from $table where sortid='$ele' limit 0,2;";
        
    }
    这样会效率低一点,但是肯定可行!