我的桌子就像......
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
当我使用此查询时 - 表名是gallery ...
$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
请帮帮我......
答案 0 :(得分:0)
试试这个:
SELECT
MAX(id),
Sub_id,
name,
path,
date
FROM
gallery
GROUP BY
Sub_id
ORDER BY
id DESC
LIMIT 4;
答案 1 :(得分: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";
答案 2 :(得分:0)
请试试这个
select * from (select max(id), sub_id from gallery group by sub_id order by id desc) where rownum <=4;