3

我的桌子就像...

id  Sub_id  name    path    date

1   33  Sanjay  Something here to all   Some thing
2   33  Sanjay      
3   34  Fariya      
4   35  Neeraj      
5   35  Neeraj      
6   32  Mukesh      
7   37  Ravi        
8   32  Mukesh      
9   32  Mukesh      
10  32  Mukesh      

当我使用此查询时-表名是画廊...

$sql="select * from (select * from gallery order by id desc 4) gallery group by Sub_id desc limit 4";

我需要这种格式

id  Sub_id  name    path    date
10  32  Mukesh  Something here to all   Some thing
7   37  Ravi        
5   35  Neeraj      
3   34  Fariya      

请帮我...

4

3 回答 3

0

以您的原始 SQL 为基础(如果您想先对 id 排序,然后对 sub_id 排序):

$sql="select * from gallery group by sub_id order by id desc, subid desc limit 0,4";

如果不打算消除“重复”,则可以使用以下 sql 代替上述 sql:

$sql="select * from gallery order by id desc, subid desc limit 0,4";
于 2012-07-30T09:34:28.727 回答
0

请试试这个

select * from  (select max(id), sub_id from gallery group by sub_id order by id desc) where rownum <=4;
于 2012-07-30T09:34:40.207 回答
0

尝试这个 :

SELECT 
    MAX(id), 
    Sub_id, 
    name, 
    path, 
    date 
FROM 
    gallery 
GROUP BY 
    Sub_id
ORDER BY 
    id DESC 
LIMIT 4;
于 2012-07-30T09:24:44.490 回答