Possible Duplicate:
How can I sort id and sub_id in descending order?可能重复:如何按降序对id和sub_id进行排序?
I have a mysql table named gallery
.
我有一个名为gallery的mysql表。
id sub_id name
1 10 jack
2 10 jack
3 11 rom
4 11 rom
5 12 win
6 12 win
7 13 shivam
8 13 shivam
There are 4 different sub_id but all have different id. So i just want to fetch only one id from every sub_id in descending order like below :
有4个不同的sub_id但都有不同的id。所以我只想从每个sub_id中获取一个id,降序如下:
id sub_id name
8 13 shivam
6 12 win
4 11 rom
2 10 jack
I used this query but it doesn't help me .
我使用了这个查询,但它对我没有帮助。
$sql="SELECT MAX(id), sub_id, name FROM gallery GROUP BY sub_id ORDER BY id DESC LIMIT 4";
2 个解决方案
#1
0
you may try by the following query
您可以尝试以下查询
$sql="SELECT MAX(id), sub_id, sub_name FROM gallery GROUP BY sub_id ORDER BY id DESC LIMIT 4";
#2
0
You should select the max "sub_id" of every group to obtain the desired result, and if the "id" is already ordered you needn't to put an order statement. So the query must look like this:
您应该选择每个组的最大“sub_id”以获得所需的结果,如果已经订购了“id”,则无需输入订单声明。所以查询必须如下所示:
SELECT MAX( `id` ) , `sub_id` , `name`
FROM `gallary`
GROUP BY `sub_id`
for the limit you can add it easily. I hope that it helps.
对于限制,您可以轻松添加它。我希望它有所帮助。
#1
0
you may try by the following query
您可以尝试以下查询
$sql="SELECT MAX(id), sub_id, sub_name FROM gallery GROUP BY sub_id ORDER BY id DESC LIMIT 4";
#2
0
You should select the max "sub_id" of every group to obtain the desired result, and if the "id" is already ordered you needn't to put an order statement. So the query must look like this:
您应该选择每个组的最大“sub_id”以获得所需的结果,如果已经订购了“id”,则无需输入订单声明。所以查询必须如下所示:
SELECT MAX( `id` ) , `sub_id` , `name`
FROM `gallary`
GROUP BY `sub_id`
for the limit you can add it easily. I hope that it helps.
对于限制,您可以轻松添加它。我希望它有所帮助。