Mysql groups the data and retrieves the most recent data

select * from AccountMark as b where not exists(select 1 from AccountMark where AccountId= b.AccountId
and b.CreateTime<CreateTime )

Thank you for the answer, the data found in this way is correct and the problem of query clauses is avoided. But if I want to count how many Marks (Num in the last screenshot) there are in each AccountId, what should I write?

select *,(select count(distinct mark) from AccountMark where AccountId= b.AccountId)
 from AccountMark as b where not exists(select 1 from AccountMark where AccountId= b.AccountId
 and b.CreateTime<CreateTime )

There are two methods below: Suppose your table is called t_biz_sign, the 'latest column' field is called create_time, and the field to be grouped is called foreign_id 

SELECT sign.*
FROM t_biz_sign sign
WHERE NOT exists(SELECT 1
                 FROM t_biz_sign
                 WHERE foreign_id = sign.foreign_id
                       AND sign .create_time < create_time)
ORDER BY create_time DESC;


SELECT *
FROM t_biz_sign sign
WHERE (SELECT count(*)
       FROM t_biz_sign AS m
       WHERE m.foreign_id = sign.foreign_id AND m.create_time >= sign.create_time) < 2;

Refer to the various methods in the following post

http://blog.csdn.net/acmain_chm/article/details/4126306
[collection] grouping to take the maximum N records method collection, and scattered....

 

Often see the question, how to get the top N records of each group. For your convenience, some common solutions are listed below.

Question: There is a table as follows, the top two of each class are required to be taken out (tie for second place is allowed)
Table1
+----+------+------+-----+
| id |SName |ClsNo |Score|
+----+------+------+-----+
| 1 |AAAA | C1 | 67 |
| 2 |BBBB | C1 | 55 |
| 3|CCCC|C1|67||
4|DDDD|C1|65||
5|EEEE|C1|95||
6|FFFF|C2|57||
7|GGGG|C2|87||
8|HHHH|C2 | 74 |
| 9 | IIII | C2 | 52 |
| 10 | JJJJ | C2 | 81 |
| 11 | KKKK | C2 | 67 | | 12
| LLLL
|
|NNNN|C3|99||
15|OOOO|C3|50|
| 16 | PPPP | C3 | 59 |
| 17 | QQQQ | C3 | 66 |
| 18 | RRRR | C3 | 76 |
| 19 | SSSS | C3
|
C3 | 64 |
| 22 |VVVV | C3 | 74 |
+----+------+------+-----+

The result is as follows
+----+--- ---+------+-----+
| id |SName |ClsNo |Score|
+----+------+------+---- -+
|5|EEEE|C1|95||
1|AAAA|C1|67||
3|CCCC|C1|67||
7|GGGG|C2|87||
10|JJJJ|C2|81
||14| NNNN|C3|99||
18|RRRR|C3|76|
+----+------+------+-----+

 

方法一:
select a.id,a.SName,a.ClsNo,a.Score
from Table1 a left join Table1 b on a.ClsNo=b.ClsNo and a.Score<b.Score
group by a.id,a.SName,a.ClsNo,a.Score
having count(b.id)<2
order by a.ClsNo,a.Score desc

 

Method Two:

 

select *
from Table1 a
where 2>(select count(*) from Table1 where ClsNo=a.ClsNo and Score>a.Score)
order by a.ClsNo,a.Score desc

 

 

 

方法三:
select *
from Table1 a
where id in (select id from Table1 where ClsNo=a.ClsNo order by Score desc limit 2)
order by a.ClsNo,a.Score desc

 

 

 

Methods....

Here are the implementation methods of various SQL statements, some of which are specific to MySQL (Limit, other databases can be changed according to the actual situation, such as Oracle 's rownum, MS SQL SERVER's top, ..), and sometimes SQL Standard supported. But the efficiency and application occasions may be different. The specific application can be selected according to the records and indexes in the actual table.

 

 

 

 

 

Special case N=1, that is, take the largest/smallest record.
+----+------+------+-----+
| id |SName |ClsNo |Score|
+----+------+-- ----+-----+
| 5 |EEEE | ​​C1 | 95 |
| 7 |GGGG | C2 | 87 |
| 14 |NNNN | C3 | 99 |
+----+------ +------+-----+

 

 

 

select *
from Table1 a
where not exists (select 1 from Table1 where ClsNo=a.ClsNo and Score>a.Score);

 

 

 

 

 

select a.*
from Table1 a inner join (select ClsNo, max(Score) as mScore from Table1 group by ClsNo) b
 on a.ClsNo=b.ClsNo and a.Score=b.Score

 


select *
from (select * from Table1 order by Score desc) t
group by ClsNo

 

This article comes from: http://bbs.csdn.net/topics/390958705

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326560709&siteId=291194637