Query from mysql to the last statement

select   *   from   table   order   by   id   desc   limit   1

MySql : SELECT * FROM 表名 ORDER BY 表_ID DESC LIMIT 1
SQLServer/Oracle : SELECT TOP 1 * FROM 表名 ORDER BY 表_ID DESC

First determine what is the last one.
Is the latest edit time the last one, or the one with the largest number in a field is not the last one.
For example, if the maximum time is the last item, all the data that meet the conditions will be filtered out, and then sorted by time, and then a piece of data will be taken.
The SQL is as follows:
select a,b from table where a>'some time' order by a desc limit 1
(a in the above SQL is the time).

Use max(time) to query! !

select oid,status,max(time) time from 表名 group by oid,max(time);
SELECT * from tb where id = (SELECT max(id) FROM tb);

mysql group to get the latest record (the entire record)

mysql fetches the latest record after grouping, the following two methods. One is to first filter out the largest and latest time, and query in the linked table. The other is to sort first, and then query in the sub-group (default first), that is The latest piece of 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  

mysql query the first few rows to the first few rows of records, query the last and first rows of records, query the first and last rows of records

1. Query the first row of records:

   select   *   from   table  limit   1

2. Query records from row n to row m

  select * from table1  limit n-1,m-n;

     SELECT * FROM table LIMIT 5,10;返回第6行到第15行的记录

     select * from employee limit 3,1; // 返回第4行

3. Query the first n rows of records

  select * from table1 limit 0,n;

or

 select * from table1 limit n;

4. N rows of records after query

 `select * from table1 order by id desc dlimit n;`//倒序排序,取前n行 id为自增形式

5. Query the next record of a record ($id)

 select * from table1 where id>$id  order by id asc dlimit 1

6. Query the previous record of a record ($id)

select * from table1 where id<$id  order by id desc dlimit 1

Guess you like

Origin blog.csdn.net/ssdssa/article/details/109007984