从mysql中查询到最后一条语句

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

首先要确定什么是最后一条。
是编辑时间最新的为最后一条,还是某个字段数字最大的未最后一条。
比如以时间最大为最后一条,则将符合条件的资料都筛选出来,再按时间排序,再取一笔资料。
SQL如下:
select a,b from table where a>‘某个时间’ order by a desc limit 1
(上面SQL中a为时间)。

用max(time)查询方可!!

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

mysql 分组取最新的一条记录(整条记录)

mysql取分组后最新的一条记录,下面两种方法.一种是先筛选 出最大和最新的时间,在连表查询.一种是先排序,然后在次分组查询(默认第一条),就是最新的一条数据了

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 查询第几行到第几行记录 查询最后一行和第一行记录 查询前几行和后几行记录

1、查询第一行记录:

   select   *   from   table  limit   1

2、查询第n行到第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、查询前n行记录

  select * from table1 limit 0,n;

 select * from table1 limit n;

4、查询后n行记录

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

5、查询一条记录($id)的下一条记录

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

6、查询一条记录($id)的上一条记录

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

猜你喜欢

转载自blog.csdn.net/ssdssa/article/details/109007984