mysql group query latest/highest record

Method: mysql takes the latest record after grouping, the following two methods. One is to filter out the largest and latest time first, and query in the connected table. , which is the latest data  
    #select * from t_assistant_article as a, (select max(base_id) as base_id, max(create_time) as create_time from t_assistant_article as b group by base_id ) as b where a.base_id=b.base_id and a.create_time = b.create_time #select base_id,max(create_time), max(article_id) as article_id from t_assistant_article as b group by base_id select * from (select * from t_assistant_article order by create_time desc) as a group by base_id order by create_time desc

来源:http://blog.csdn.net/swweb/article/details/11059037

Research on mysql "group by" and "order by" - the latest content in the classification http://www.jb51.net/article/23969.htm


When using mysql sorting, you will think of grouping in descending order to obtain a set of data, but using order by often does not get the ideal results, so how can you use group by and order by to get the ideal data results?

For example, there is a reply table for posts, posts ( id , tid , subject , message , dateline ) ,

id is the auto-incrementing field, tid is the id of the subject post of the reply (foreign key association), subject is the reply title, message is the reply content, dateline is the reply time, expressed as a UNIX timestamp,

Now asked to pick the top ten most recent replies from different topics


SELECT * FROM posts GROUP BY  tid  LIMIT 10


Such an sql statement selects not the latest reply you want, but the earliest reply, which is actually the first reply record of a certain topic!

That is to say, the GROUP BY statement is not sorted, so how can GROUP be arranged in reverse dateline order? Add an order by clause?

look down:

SELECT * FROM posts GROUP BY  tid  ORDER BY dateline DESC LIMIT 10


The result selected by this statement is exactly the same as the above, but the results are arranged in reverse order, and each record selected is still the above record, the reason is that group by will be executed before order by, so there is no way to Before group by, that is, sorting before grouping, some netizens will write the following sql statement:


SELECT * FROM posts GROUP BY  tid DESC ORDER BY dateline DESC LIMIT 10


That is to say, add a descending order after the field tid of GROUP BY, so that you can get the last reply when grouping? The execution result of this statement will be exactly the same as above, and adding DESC and ASC here has no effect on the execution result! In fact, this is a wrong statement. The reason is that there is no sorting function before GROUP BY. The mysql manual says that GROUP BY is sorted in a certain order. What is the order of a certain order? In fact, there is no order at all, because grouping according to tid, in fact, that is to say, grouping equal tids into a group, in this way, GROUP BY tid DESC can be considered to be in reverse order according to tid when grouping according to tid, which does not mean What's wrong, since it is grouped according to tid, of course, the tids are equal to one group, and at this time, there is a P for use in flashback or ascending order according to tid!


So some netizens invented the following sentence:


SELECT * FROM posts GROUP BY  tid , dateline DESC ORDER BY dateline DESC LIMIT 10


I thought that I could arrange the dateline in reverse order before grouping. In fact, this statement did not play the role of grouping by tid. The reason is the above. It is wrong to add desc or asc after the group by field. The netizen's original intention is to group by tid, and in reverse order by dateline when grouping! In fact, this sentence is equivalent to the following writing: (remove the DESC after the GROUP BY field)


SELECT * FROM posts GROUP BY  tid , dateline ORDER BY dateline DESC LIMIT 10


That is to say, according to the joint grouping of tid and dateline, only when the record tid and dateline are equal at the same time can they be grouped into one group, which is obviously impossible, because the dateline timeline is basically unique!


Someone wrote the following statement:


SELECT *,max(dateline) as max_line FROM posts GROUP BY  tid ORDER BY dateline DESC LIMIT 10


This statement is correct that the maximum release time is selected, but you can compare the dateline and max_dateline are not equal! (There may be quite a situation, that is, when there is only one target record of the group!)


why? The reason is very simple, this statement is equivalent to selecting the largest release time of this group after group by! No effect on grouping! Because the SELECT clause is executed last!

Later, more netizens invented the following writing method!


SELECT *,max(dateline) as max_line FROM posts GROUP BY  tid HAVING dateline=max(dateline) ORDER BY dateline DESC LIMIT 10


The expected result of this statement is not as expected! Because you will find that a large number of records in the grouped results are gone! Why? Because HAVING is executed when grouping, that is to say: add a condition when grouping: the selected dateline must be equal to the largest dateline in the group, and the execution result is the same as the following statement:


SELECT *,max(dateline) as max_line FROM posts GROUP BY  tid HAVING count(*)=1 ORDER BY dateline DESC LIMIT 10


Do you understand after reading this sql statement?

dateline=max(dateline) is only established when there is only one record in the group, the reason is very clear! Only one of them will be equal to the maximum release time of this group, (the default dateline is a unique value)


The reason is because group by does not have a sorting function. All these sorting functions are just illusions, so the dateline and max(dateline) you finally select can never be equal, unless there is only one record in this group! When GROUP BY is grouping, it may be looking for one by one, and it finds that there are equal tids, remove it, and keep the first found record, so the found records are always arranged in the order of the default index!


So having said so much, is there any way to make group by execute before grouping? Yes, sub-query!


the simplest:


SELECT * FROM (SELECT * FROM posts ORDER BY dateline DESC) GROUP BY  tid ORDER BY dateline DESC LIMIT 10


Some netizens also use self-connection to achieve this. This efficiency should be higher than the above sub-query. However, for simplicity and clarity, only this one is used. GROUP BY does not have a sorting function. haven't found it yet,

 

Original link: https://www.cnblogs.com/Alight/p/3425357.html

Method: mysql takes the latest record after grouping, the following two methods. One is to filter out the largest and latest time first, and query in the connected table. , which is the latest data  
    #select * from t_assistant_article as a, (select max(base_id) as base_id, max(create_time) as create_time from t_assistant_article as b group by base_id ) as b where a.base_id=b.base_id and a.create_time = b.create_time #select base_id,max(create_time), max(article_id) as article_id from t_assistant_article as b group by base_id select * from (select * from t_assistant_article order by create_time desc) as a group by base_id order by create_time desc

来源:http://blog.csdn.net/swweb/article/details/11059037

Research on mysql "group by" and "order by" - the latest content in the classification http://www.jb51.net/article/23969.htm


When using mysql sorting, you will think of grouping in descending order to obtain a set of data, but using order by often does not get the ideal results, so how can you use group by and order by to get the ideal data results?

For example, there is a reply table for posts, posts ( id , tid , subject , message , dateline ) ,

id is the auto-incrementing field, tid is the id of the subject post of the reply (foreign key association), subject is the reply title, message is the reply content, dateline is the reply time, expressed as a UNIX timestamp,

Now asked to pick the top ten most recent replies from different topics


SELECT * FROM posts GROUP BY  tid  LIMIT 10


Such an sql statement selects not the latest reply you want, but the earliest reply, which is actually the first reply record of a certain topic!

That is to say, the GROUP BY statement is not sorted, so how can GROUP be arranged in reverse dateline order? Add an order by clause?

look down:

SELECT * FROM posts GROUP BY  tid  ORDER BY dateline DESC LIMIT 10


The result selected by this statement is exactly the same as the above, but the results are arranged in reverse order, and each record selected is still the above record, the reason is that group by will be executed before order by, so there is no way to Before group by, that is, sorting before grouping, some netizens will write the following sql statement:


SELECT * FROM posts GROUP BY  tid DESC ORDER BY dateline DESC LIMIT 10


That is to say, add a descending order after the field tid of GROUP BY, so that you can get the last reply when grouping? The execution result of this statement will be exactly the same as above, and adding DESC and ASC here has no effect on the execution result! In fact, this is a wrong statement. The reason is that there is no sorting function before GROUP BY. The mysql manual says that GROUP BY is sorted in a certain order. What is the order of a certain order? In fact, there is no order at all, because grouping according to tid, in fact, that is to say, grouping equal tids into a group, in this way, GROUP BY tid DESC can be considered to be in reverse order according to tid when grouping according to tid, which does not mean What's wrong, since it is grouped according to tid, of course, the tids are equal to one group, and at this time, there is a P for use in flashback or ascending order according to tid!


So some netizens invented the following sentence:


SELECT * FROM posts GROUP BY  tid , dateline DESC ORDER BY dateline DESC LIMIT 10


I thought that I could arrange the dateline in reverse order before grouping. In fact, this statement did not play the role of grouping by tid. The reason is the above. It is wrong to add desc or asc after the group by field. The netizen's original intention is to group by tid, and in reverse order by dateline when grouping! In fact, this sentence is equivalent to the following writing: (remove the DESC after the GROUP BY field)


SELECT * FROM posts GROUP BY  tid , dateline ORDER BY dateline DESC LIMIT 10


That is to say, according to the joint grouping of tid and dateline, only when the record tid and dateline are equal at the same time can they be grouped into one group, which is obviously impossible, because the dateline timeline is basically unique!


Someone wrote the following statement:


SELECT *,max(dateline) as max_line FROM posts GROUP BY  tid ORDER BY dateline DESC LIMIT 10


This statement is correct that the maximum release time is selected, but you can compare the dateline and max_dateline are not equal! (There may be quite a situation, that is, when there is only one target record of the group!)


why? The reason is very simple, this statement is equivalent to selecting the largest release time of this group after group by! No effect on grouping! Because the SELECT clause is executed last!

Later, more netizens invented the following writing method!


SELECT *,max(dateline) as max_line FROM posts GROUP BY  tid HAVING dateline=max(dateline) ORDER BY dateline DESC LIMIT 10


The expected result of this statement is not as expected! Because you will find that a large number of records in the grouped results are gone! Why? Because HAVING is executed when grouping, that is to say: add a condition when grouping: the selected dateline must be equal to the largest dateline in the group, and the execution result is the same as the following statement:


SELECT *,max(dateline) as max_line FROM posts GROUP BY  tid HAVING count(*)=1 ORDER BY dateline DESC LIMIT 10


Do you understand after reading this sql statement?

dateline=max(dateline) is only established when there is only one record in the group, the reason is very clear! Only one of them will be equal to the maximum release time of this group, (the default dateline is a unique value)


The reason is because group by does not have a sorting function. All these sorting functions are just illusions, so the dateline and max(dateline) you finally select can never be equal, unless there is only one record in this group! When GROUP BY is grouping, it may be looking for one by one, and it finds that there are equal tids, remove it, and keep the first found record, so the found records are always arranged in the order of the default index!


So having said so much, is there any way to make group by execute before grouping? Yes, sub-query!


the simplest:


SELECT * FROM (SELECT * FROM posts ORDER BY dateline DESC) GROUP BY  tid ORDER BY dateline DESC LIMIT 10


Some netizens also use self-connection to achieve this. This efficiency should be higher than the above sub-query. However, for simplicity and clarity, only this one is used. GROUP BY does not have a sorting function. haven't found it yet,

 

Original link: https://www.cnblogs.com/Alight/p/3425357.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325084266&siteId=291194637