mysql query the latest first record problem summary

Summary of the latest record in the mysql query table

When using a database, we often encounter the following problems: For
example, there is a table A with the following fields and data:

name ID number (unique identifier) ​​(id) buy product (pro) price number (count) Purchase time (time)
Zhang 1 111111 Computer 1600 yuan 5 2018-03-03
Zhang 1 111111 Phone $12 12 2018-03-05
Zhang 2 222222 Pipe 1 Yuan 234 2018-03-04
Zhang 2 222222 Computer 1600 yuan 5 2018-03-05
Zhang 3 333333 Phone $12 12 2018-03-03
Zhang 3 333333 Pipe 1 Yuan 234 2018-03-06
Zhang 3 333333 Computer 1600 yuan 5 2018-03-08
Zhang 4 444444 Phone $12 12 2018-03-09
Zhang 5 555555 Pipe 1 Yuan 234 2018-03-02

At this point, I need to query what products and product-related information each user purchased last time in the table.
There are three specific implementation methods:

Method 1: The easiest way to implement;

select * from ( select * from A order by time ) a group by a.id

Explanation:
First, sort the A table according to time, and then nest a layer of query after sorting. This layer of query uses the group by statement. When using the group by statement, it will take out the first item of your sorted data according to the group, so that it meets the conditions.
After experiments, this method is more efficient to use when adding an index. After testing, the query time of 60,000 data is about three seconds.

Method 2: Use the method of internal association; Explanation: This method is implemented using internal association; when two tables are internally associated, the database does not use a certain table as the basis, and directly queries the data with the same associated fields; for table temp It is said that the result of the query from this table is that the id is unique and the maximum time corresponding to the id, and the associated fields are id and time, so when it is associated with table A, the id and maximum time in table A will be queried, so the matching result will be meet our requirements. However, there is a problem with this query method, that is, when there is dirty data, for example, there are two pieces of 1 data, and its time, id, and other fields are all the same, then the number of results queried is incorrect; but using method 1 only There will be no such problem.

select * from A inner join ( select id,max(time) as 'tempTime' from A group by id ) temp on temp.id = A.id and temp.tempTime = time


Method 3: Use not exists:

select * from A as a1 where not exists(select 1 from A as a2 where a1.id =a2.id
and a2.time<a1.time)

Explanation: This method can compare its own time with its own time, and query the results through time comparison. There are two problems in this implementation method. First,
if
there is dirty data in the business data, the number of query results is incorrect. Second, the query time is long, and the query method needs to use 30s+ for a table with
50,000 data;

Guess you like

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