求个分组查询的sql,谢了先

若鱼1919 2016-02-26 11:45:04
id no value time
1 1 1 2016-02-26 10:10:10
2 2 5 2016-02-26 10:11:10
3 1 10 2016-02-26 10:12:10
4 1 11 2016-02-26 10:13:10
5 2 8 2016-02-26 10:14:10
6 1 7 2016-02-26 10:17:10
7 1 3 2016-02-26 10:18:10
8 3 5 2016-02-26 10:19:10
9 3 1 2016-02-26 10:20:10
结果:
id no value time
7 1 3 2016-02-26 10:18:10
5 2 8 2016-02-26 10:14:10
9 3 1 2016-02-26 10:20:10
先按照no分组,然后找到每一个组里面time最近的列,结果是以no升序拍
...全文
175 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
若鱼1919 2016-03-01
  • 打赏
  • 举报
回复
引用 9 楼 yupeigu 的回复:
[quote=引用 8 楼 goldenfish1919 的回复:]

select * from t3 
right join (
	SELECT no,max(time) time FROM t3
	group by no
	order by no asc
)tt 
on t3.no = tt.no and t3.time = tt.time
我是这么弄的
对,你这么弄也对的,也是一种好的方法, order by no asc 这个可以去掉,可以提高性能。 另外,这种写法在 同一个no,最近时间time有多个相同的时候,会返回多条数据,所以会有问题。[/quote] 学习了!!!!
若鱼1919 2016-02-29
  • 打赏
  • 举报
回复

select * from t3 
right join (
	SELECT no,max(time) time FROM t3
	group by no
	order by no asc
)tt 
on t3.no = tt.no and t3.time = tt.time
我是这么弄的
若鱼1919 2016-02-29
  • 打赏
  • 举报
回复
引用 3 楼 yupeigu 的回复:
完整的:
mysql> create table t3
    -> (
    -> id  int,
    -> no int,
    -> value int,
    -> time datetime
    -> );
Query OK, 0 rows affected (4.97 sec)

mysql>
mysql> insert into t3
    -> select 1,    1,  1,    '2016-02-26 10:10:10' union all
    -> select 2,    2,  5,     '2016-02-26 10:11:10' union all
    -> select 3,    1,  10,    '2016-02-26 10:12:10' union all
    -> select 4,    1,  11,    '2016-02-26 10:13:10' union all
    -> select 5,    2,  8,    '2016-02-26 10:14:10' union all
    -> select 6,    1,  7,    '2016-02-26 10:17:10' union all
    -> select 7,    1,  3,    '2016-02-26 10:18:10' union all
    -> select 8,    3,  5,    '2016-02-26 10:19:10' union all
    -> select 9,    3,  1,    '2016-02-26 10:20:10';
Query OK, 9 rows affected (1.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> select * from t3;
+------+------+-------+---------------------+
| id   | no   | value | time                |
+------+------+-------+---------------------+
|    1 |    1 |     1 | 2016-02-26 10:10:10 |
|    2 |    2 |     5 | 2016-02-26 10:11:10 |
|    3 |    1 |    10 | 2016-02-26 10:12:10 |
|    4 |    1 |    11 | 2016-02-26 10:13:10 |
|    5 |    2 |     8 | 2016-02-26 10:14:10 |
|    6 |    1 |     7 | 2016-02-26 10:17:10 |
|    7 |    1 |     3 | 2016-02-26 10:18:10 |
|    8 |    3 |     5 | 2016-02-26 10:19:10 |
|    9 |    3 |     1 | 2016-02-26 10:20:10 |
+------+------+-------+---------------------+
9 rows in set (0.01 sec)

mysql> select id,no,value,time
    -> from
    -> (
    -> select *
    -> from t3
    -> order by no,time desc
    -> ) t
    -> group by no;
+------+------+-------+---------------------+
| id   | no   | value | time                |
+------+------+-------+---------------------+
|    7 |    1 |     3 | 2016-02-26 10:18:10 |
|    5 |    2 |     8 | 2016-02-26 10:14:10 |
|    9 |    3 |     1 | 2016-02-26 10:20:10 |
+------+------+-------+---------------------+
3 rows in set (0.11 sec)
大神,跪了!
LongRui888 2016-02-29
  • 打赏
  • 举报
回复
引用 4 楼 chenlianghaodai 的回复:
[quote=引用 3 楼 yupeigu 的回复:] 完整的:
mysql> create table t3
    -> (
    -> id  int,
    -> no int,
    -> value int,
    -> time datetime
    -> );
Query OK, 0 rows affected (4.97 sec)

mysql>
mysql> insert into t3
    -> select 1,    1,  1,    '2016-02-26 10:10:10' union all
    -> select 2,    2,  5,     '2016-02-26 10:11:10' union all
    -> select 3,    1,  10,    '2016-02-26 10:12:10' union all
    -> select 4,    1,  11,    '2016-02-26 10:13:10' union all
    -> select 5,    2,  8,    '2016-02-26 10:14:10' union all
    -> select 6,    1,  7,    '2016-02-26 10:17:10' union all
    -> select 7,    1,  3,    '2016-02-26 10:18:10' union all
    -> select 8,    3,  5,    '2016-02-26 10:19:10' union all
    -> select 9,    3,  1,    '2016-02-26 10:20:10';
Query OK, 9 rows affected (1.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> select * from t3;
+------+------+-------+---------------------+
| id   | no   | value | time                |
+------+------+-------+---------------------+
|    1 |    1 |     1 | 2016-02-26 10:10:10 |
|    2 |    2 |     5 | 2016-02-26 10:11:10 |
|    3 |    1 |    10 | 2016-02-26 10:12:10 |
|    4 |    1 |    11 | 2016-02-26 10:13:10 |
|    5 |    2 |     8 | 2016-02-26 10:14:10 |
|    6 |    1 |     7 | 2016-02-26 10:17:10 |
|    7 |    1 |     3 | 2016-02-26 10:18:10 |
|    8 |    3 |     5 | 2016-02-26 10:19:10 |
|    9 |    3 |     1 | 2016-02-26 10:20:10 |
+------+------+-------+---------------------+
9 rows in set (0.01 sec)

mysql> select id,no,value,time
    -> from
    -> (
    -> select *
    -> from t3
    -> order by no,time desc
    -> ) t
    -> group by no;
+------+------+-------+---------------------+
| id   | no   | value | time                |
+------+------+-------+---------------------+
|    7 |    1 |     3 | 2016-02-26 10:18:10 |
|    5 |    2 |     8 | 2016-02-26 10:14:10 |
|    9 |    3 |     1 | 2016-02-26 10:20:10 |
+------+------+-------+---------------------+
3 rows in set (0.11 sec)
版主大大,像你这种查询写法在oracle中是行不通的吧? 每个no对应了多个其他字段,在mysql中直接group by 查询为何显示的是第一条,按照oracl的语法思路,是会报错的!这是mysql的特性吗?[/quote] 对的,这个是mysql的特性,在oracle或者sql server里是违反语法规则的,所以会直接报错,因为只有在group by中出现的字段,才能在select中出现,否则就只能把字段放到聚合函数中,比如max,count等。 如果你要用oracle来实现,可以更加简单,oracle里支持row_number函数,非常方便,且性能更高,此语法在oracle,sql server、db2中都是通用的,但是现在mysql还不支持这种写法:
select *
from 
(
select *,
       row_number() over(partition by no order by time desc) rn
from t3
)t
where rn = 1
LongRui888 2016-02-29
  • 打赏
  • 举报
回复
引用 8 楼 goldenfish1919 的回复:

select * from t3 
right join (
	SELECT no,max(time) time FROM t3
	group by no
	order by no asc
)tt 
on t3.no = tt.no and t3.time = tt.time
我是这么弄的
对,你这么弄也对的,也是一种好的方法, order by no asc 这个可以去掉,可以提高性能。 另外,这种写法在 同一个no,最近时间time有多个相同的时候,会返回多条数据,所以会有问题。
若鱼1919 2016-02-26
  • 打赏
  • 举报
回复
ACMAIN_CHM 2016-02-26
  • 打赏
  • 举报
回复
参考下贴中的多种方法 http://blog.csdn.net/acmain_chm/article/details/4126306 [征集]分组取最大N条记录方法征集,及散分....
chenlianghaodai 2016-02-26
  • 打赏
  • 举报
回复
引用 3 楼 yupeigu 的回复:
完整的:
mysql> create table t3
    -> (
    -> id  int,
    -> no int,
    -> value int,
    -> time datetime
    -> );
Query OK, 0 rows affected (4.97 sec)

mysql>
mysql> insert into t3
    -> select 1,    1,  1,    '2016-02-26 10:10:10' union all
    -> select 2,    2,  5,     '2016-02-26 10:11:10' union all
    -> select 3,    1,  10,    '2016-02-26 10:12:10' union all
    -> select 4,    1,  11,    '2016-02-26 10:13:10' union all
    -> select 5,    2,  8,    '2016-02-26 10:14:10' union all
    -> select 6,    1,  7,    '2016-02-26 10:17:10' union all
    -> select 7,    1,  3,    '2016-02-26 10:18:10' union all
    -> select 8,    3,  5,    '2016-02-26 10:19:10' union all
    -> select 9,    3,  1,    '2016-02-26 10:20:10';
Query OK, 9 rows affected (1.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> select * from t3;
+------+------+-------+---------------------+
| id   | no   | value | time                |
+------+------+-------+---------------------+
|    1 |    1 |     1 | 2016-02-26 10:10:10 |
|    2 |    2 |     5 | 2016-02-26 10:11:10 |
|    3 |    1 |    10 | 2016-02-26 10:12:10 |
|    4 |    1 |    11 | 2016-02-26 10:13:10 |
|    5 |    2 |     8 | 2016-02-26 10:14:10 |
|    6 |    1 |     7 | 2016-02-26 10:17:10 |
|    7 |    1 |     3 | 2016-02-26 10:18:10 |
|    8 |    3 |     5 | 2016-02-26 10:19:10 |
|    9 |    3 |     1 | 2016-02-26 10:20:10 |
+------+------+-------+---------------------+
9 rows in set (0.01 sec)

mysql> select id,no,value,time
    -> from
    -> (
    -> select *
    -> from t3
    -> order by no,time desc
    -> ) t
    -> group by no;
+------+------+-------+---------------------+
| id   | no   | value | time                |
+------+------+-------+---------------------+
|    7 |    1 |     3 | 2016-02-26 10:18:10 |
|    5 |    2 |     8 | 2016-02-26 10:14:10 |
|    9 |    3 |     1 | 2016-02-26 10:20:10 |
+------+------+-------+---------------------+
3 rows in set (0.11 sec)
版主大大,像你这种查询写法在oracle中是行不通的吧? 每个no对应了多个其他字段,在mysql中直接group by 查询为何显示的是第一条,按照oracl的语法思路,是会报错的!这是mysql的特性吗?
LongRui888 2016-02-26
  • 打赏
  • 举报
回复
完整的:
mysql> create table t3
    -> (
    -> id  int,
    -> no int,
    -> value int,
    -> time datetime
    -> );
Query OK, 0 rows affected (4.97 sec)

mysql>
mysql> insert into t3
    -> select 1,    1,  1,    '2016-02-26 10:10:10' union all
    -> select 2,    2,  5,     '2016-02-26 10:11:10' union all
    -> select 3,    1,  10,    '2016-02-26 10:12:10' union all
    -> select 4,    1,  11,    '2016-02-26 10:13:10' union all
    -> select 5,    2,  8,    '2016-02-26 10:14:10' union all
    -> select 6,    1,  7,    '2016-02-26 10:17:10' union all
    -> select 7,    1,  3,    '2016-02-26 10:18:10' union all
    -> select 8,    3,  5,    '2016-02-26 10:19:10' union all
    -> select 9,    3,  1,    '2016-02-26 10:20:10';
Query OK, 9 rows affected (1.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> select * from t3;
+------+------+-------+---------------------+
| id   | no   | value | time                |
+------+------+-------+---------------------+
|    1 |    1 |     1 | 2016-02-26 10:10:10 |
|    2 |    2 |     5 | 2016-02-26 10:11:10 |
|    3 |    1 |    10 | 2016-02-26 10:12:10 |
|    4 |    1 |    11 | 2016-02-26 10:13:10 |
|    5 |    2 |     8 | 2016-02-26 10:14:10 |
|    6 |    1 |     7 | 2016-02-26 10:17:10 |
|    7 |    1 |     3 | 2016-02-26 10:18:10 |
|    8 |    3 |     5 | 2016-02-26 10:19:10 |
|    9 |    3 |     1 | 2016-02-26 10:20:10 |
+------+------+-------+---------------------+
9 rows in set (0.01 sec)

mysql> select id,no,value,time
    -> from
    -> (
    -> select *
    -> from t3
    -> order by no,time desc
    -> ) t
    -> group by no;
+------+------+-------+---------------------+
| id   | no   | value | time                |
+------+------+-------+---------------------+
|    7 |    1 |     3 | 2016-02-26 10:18:10 |
|    5 |    2 |     8 | 2016-02-26 10:14:10 |
|    9 |    3 |     1 | 2016-02-26 10:20:10 |
+------+------+-------+---------------------+
3 rows in set (0.11 sec)
LongRui888 2016-02-26
  • 打赏
  • 举报
回复
引用 1 楼 goldenfish1919 的回复:
这么写:
select id,no,value,time
from 
(
select *
from t3
order by no,time desc
) t
group by no;

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧