先b字段排序,后a字段降序,就可以了。
select a,b from tablename group by b, a desc;
#5
select * from tablename order by a desc,b; order by两个参数 他就会先按照第一个排序 然后按照第二个
#6
a和b都是int类型 要按照a字段的值降序排列,但是b字段里一样的值要放在一起
是按照A和B同时排序?
是的话按照4L的就可以了
#7
粘贴一个想要的结果出来
#8
就是将a字段分组然后a按照b排序 结果应该是这样的
a b
1 25
1 20
1 19
3 21
3 16
2 18
2 17
#9
select * from t_test t where t.a in (
select distinct(tab.a) from (select a,max(b) from t_test group by b ORDER BY MAX(b) desc) tab
) order by field(t.a, 1,3,2);
我的思路:首先按b的 最高数找出a 的顺序
其实中间那句子查询
select distinct(tab.a) from (select a,max(b) from t_test group by b ORDER BY MAX(b) desc) tab
if object_id('tempdb.dbo.#tb') is not null drop table #tb
create table #tb (a int,b int)
insert into #tb
select 1,25 union all
select 3,21 union all
select 2,18 union all
select 1,20 union all
select 1,19 union all
select 3,16 union all
select 2,17
--select * from #tb
select a.a,a.b
from #tb a
join
(
select a,b,rn=ROW_NUMBER() over(order by b desc)
from (select a,b=MAX(b) from #tb group by a) t
) b on a.a=b.a
order by b.rn,a.b desc
/*
a b
----------
1 25
1 20
1 19
3 21
3 16
2 18
2 17
*/
#11
;with data (a, b) as
(
select 1, 19 union all
select 1, 20 union all
select 1, 25 union all
select 2, 17 union all
select 2, 18 union all
select 3, 16 union all
select 3, 21
)
, data2 as
(
select a, MAX(b)as min_b from data group by a
)
--select a,b from data group by b, a desc;
select data.* from data2
inner join data on data.a = data2.a
order by data2.min_b desc, data.b desc
#12
该回复于2016-02-25 08:34:14被管理员删除
#13
今天一大早醒来,有一种解决方案了,换个写法
select t.a,t.b from t_test t right JOIN
(select distinct(tab.a) as A from (select a,max(b) from t_test group by b ORDER BY MAX(b) desc) tab) re on t.a = re.A;
-----------------------------
a b
1 25
1 16
1 6
3 24
3 21
3 2
2 22
2 11
#14
declare @tb table(a int,b int);
insert into @tb
select 1,25 union all
select 3,21 union all
select 2,18 union all
select 1,20 union all
select 1,19 union all
select 3,16 union all
select 2,17;
select tb1.* from @tb tb1,(select a,row= ROW_NUMBER()over(order by max(b) desc) from @tb group by a )tb2 where tb1.a=tb2.a order by row
SELECT test1.a,test2.b from( select a,max(b) FROM test GROUP BY a ORDER BY max(b) desc) as test1
LEFT JOIN (SELECT * from test ORDER BY b desc ) as test2 ON test1.a = test2.a
#19
该回复于2016-02-25 21:48:21被版主删除
#20
该回复于2016-05-06 08:41:46被管理员删除
#21
#22
回帖拿分!!!!
#23
我们开发都不用sql
#24
正是我需要的,可以用,谢谢
#25
select * from table group by b order by a desc;
#26
先排序a,得到全部数据,作为一个集合,然后取值
#27
涨姿势啦,没事多看看
#28
先b字段排序,后a字段降序,就可以了。
select a,b from tablename group by b, a desc;
顶个
#29
先b字段排序,后a字段降序,就可以了。
select a,b from tablename group by b, a desc;
顶个
select b from tablename group by b order by a desc;
#30
先b字段排序,后a字段降序,就可以了。
select a,b from tablename group by b, a desc;
顶个
select b from tablename group by b order by a desc;
CREATE TABLE student (
id INT DEFAULT NULL,
name VARCHAR(10) DEFAULT NULL,
age INT DEFAULT 0,
grade INT DEFAULT 1
) ENGINE=INNODB DEFAULT CHARSET=UTF8;
select * from tablename order by a desc,b; order by两个参数 他就会先按照第一个排序 然后按照第二个
是这个样子
#32
两个小小的要求,查询的这么复杂...
#33
我在mysql中使用这个语句不知是不是你要的:
#34
SELECT * FROM test WHERE bid IN (SELECT bid FROM test GROUP BY bid ) ORDER BY aid DESC ;
#35
结果就是这样:aid bid
5 66
4 55
3 55
2 22
1 22
你试下行不行?
#36
你这个一条记录之间不同字段就不是一个整体了呀,,,不然岂不是成了一个矛盾问题
#37
首先谢谢大家的解答m(_)m
没想到有这么多,过了个周末我就忘记这里了嘿
这个问题我后来解决了,可能是我描述的不是很清楚吧
需求其实就是一个商品收藏夹的列表:1、首先商品要按照店铺分组,也就是一个店铺里的商品要放在一起
2、然后每个店铺里的商品要按照修改时间降序排列
3、每个店铺之间的排列顺序也要按照每个店铺里最新的修改时间降序排列
然后解决方法:
数据库:
然后sql语句查出分组:select storeId from(select storeId,modify from a order by modify desc) temp group by storeId order by modify desc
结果是:
然后根据分组再在代码里面循环就行了
然后用一条SQL语句怎么解决我至今也不会
#38
上面的问题补充一下,要加另一条sql:select * from a order by storeId , modify desc
把查出来的结果再按照上面那条sql语句查出来的结果循环就可以得到最终结果了
可能有更好的解决办法吧,坐等大神一天然后乖乖结贴
#39
突然发现上面有答案来着,
#1
这个还真不知道呢?
#2
如果按照你这么说的话,结果的一行数据都不是数据库里对应的一条数据了吧。
#3
楼主这是在培训的试题? 你描述的是不是有点问题?
#4
先b字段排序,后a字段降序,就可以了。
select a,b from tablename group by b, a desc;
#5
select * from tablename order by a desc,b; order by两个参数 他就会先按照第一个排序 然后按照第二个
#6
a和b都是int类型 要按照a字段的值降序排列,但是b字段里一样的值要放在一起
是按照A和B同时排序?
是的话按照4L的就可以了
#7
粘贴一个想要的结果出来
#8
a和b都是int类型 要按照a字段的值降序排列,但是b字段里一样的值要放在一起
是按照A和B同时排序?
是的话按照4L的就可以了
就是将a字段分组然后a按照b排序 结果应该是这样的
a b
1 25
1 20
1 19
3 21
3 16
2 18
2 17
#9
select * from t_test t where t.a in (
select distinct(tab.a) from (select a,max(b) from t_test group by b ORDER BY MAX(b) desc) tab
) order by field(t.a, 1,3,2);
我的思路:首先按b的 最高数找出a 的顺序
其实中间那句子查询
select distinct(tab.a) from (select a,max(b) from t_test group by b ORDER BY MAX(b) desc) tab
if object_id('tempdb.dbo.#tb') is not null drop table #tb
create table #tb (a int,b int)
insert into #tb
select 1,25 union all
select 3,21 union all
select 2,18 union all
select 1,20 union all
select 1,19 union all
select 3,16 union all
select 2,17
--select * from #tb
select a.a,a.b
from #tb a
join
(
select a,b,rn=ROW_NUMBER() over(order by b desc)
from (select a,b=MAX(b) from #tb group by a) t
) b on a.a=b.a
order by b.rn,a.b desc
/*
a b
----------
1 25
1 20
1 19
3 21
3 16
2 18
2 17
*/
#11
;with data (a, b) as
(
select 1, 19 union all
select 1, 20 union all
select 1, 25 union all
select 2, 17 union all
select 2, 18 union all
select 3, 16 union all
select 3, 21
)
, data2 as
(
select a, MAX(b)as min_b from data group by a
)
--select a,b from data group by b, a desc;
select data.* from data2
inner join data on data.a = data2.a
order by data2.min_b desc, data.b desc
#12
该回复于2016-02-25 08:34:14被管理员删除
#13
今天一大早醒来,有一种解决方案了,换个写法
select t.a,t.b from t_test t right JOIN
(select distinct(tab.a) as A from (select a,max(b) from t_test group by b ORDER BY MAX(b) desc) tab) re on t.a = re.A;
-----------------------------
a b
1 25
1 16
1 6
3 24
3 21
3 2
2 22
2 11
#14
declare @tb table(a int,b int);
insert into @tb
select 1,25 union all
select 3,21 union all
select 2,18 union all
select 1,20 union all
select 1,19 union all
select 3,16 union all
select 2,17;
select tb1.* from @tb tb1,(select a,row= ROW_NUMBER()over(order by max(b) desc) from @tb group by a )tb2 where tb1.a=tb2.a order by row
#15
今天一大早醒来,有一种解决方案了,换个写法
select t.a,t.b from t_test t right JOIN
(select distinct(tab.a) as A from (select a,max(b) from t_test group by b ORDER BY MAX(b) desc) tab) re on t.a = re.A;
select t.a,t.b from t_test t right JOIN
(select distinct(tab.a) as A from (select a,max(b) from t_test group by b ORDER BY MAX(b) desc) tab) re on t.a = re.A;
SELECT test1.a,test2.b from( select a,max(b) FROM test GROUP BY a ORDER BY max(b) desc) as test1
LEFT JOIN (SELECT * from test ORDER BY b desc ) as test2 ON test1.a = test2.a
#19
该回复于2016-02-25 21:48:21被版主删除
#20
该回复于2016-05-06 08:41:46被管理员删除
#21
#22
回帖拿分!!!!
#23
我们开发都不用sql
#24
正是我需要的,可以用,谢谢
#25
select * from table group by b order by a desc;
#26
先排序a,得到全部数据,作为一个集合,然后取值
#27
涨姿势啦,没事多看看
#28
先b字段排序,后a字段降序,就可以了。
select a,b from tablename group by b, a desc;
顶个
#29
先b字段排序,后a字段降序,就可以了。
select a,b from tablename group by b, a desc;
顶个
select b from tablename group by b order by a desc;
#30
先b字段排序,后a字段降序,就可以了。
select a,b from tablename group by b, a desc;
顶个
select b from tablename group by b order by a desc;
CREATE TABLE student (
id INT DEFAULT NULL,
name VARCHAR(10) DEFAULT NULL,
age INT DEFAULT 0,
grade INT DEFAULT 1
) ENGINE=INNODB DEFAULT CHARSET=UTF8;
select * from tablename order by a desc,b; order by两个参数 他就会先按照第一个排序 然后按照第二个
是这个样子
#32
两个小小的要求,查询的这么复杂...
#33
我在mysql中使用这个语句不知是不是你要的:
#34
SELECT * FROM test WHERE bid IN (SELECT bid FROM test GROUP BY bid ) ORDER BY aid DESC ;
#35
结果就是这样:aid bid
5 66
4 55
3 55
2 22
1 22
你试下行不行?
#36
你这个一条记录之间不同字段就不是一个整体了呀,,,不然岂不是成了一个矛盾问题
#37
首先谢谢大家的解答m(_)m
没想到有这么多,过了个周末我就忘记这里了嘿
这个问题我后来解决了,可能是我描述的不是很清楚吧
需求其实就是一个商品收藏夹的列表:1、首先商品要按照店铺分组,也就是一个店铺里的商品要放在一起
2、然后每个店铺里的商品要按照修改时间降序排列
3、每个店铺之间的排列顺序也要按照每个店铺里最新的修改时间降序排列
然后解决方法:
数据库:
然后sql语句查出分组:select storeId from(select storeId,modify from a order by modify desc) temp group by storeId order by modify desc
结果是:
然后根据分组再在代码里面循环就行了
然后用一条SQL语句怎么解决我至今也不会
#38
上面的问题补充一下,要加另一条sql:select * from a order by storeId , modify desc
把查出来的结果再按照上面那条sql语句查出来的结果循环就可以得到最终结果了
可能有更好的解决办法吧,坐等大神一天然后乖乖结贴