MySQL基本查询

1、查询某数据库中表的总数

select count(*) from information_schema.tables where table_schema='dbname';

2、仅占一列显示某数据库中的所有表名

select group_concat(table_name) from information_schema.tables where table_schema=‘dbname’;

3、查询某表中有几行

select count(*) from information_schema.COLUMNS where table_schema = 'dbname' AND table_name = 'tablename';

 4、仅占一列显示某表中的所有字段名

select group_concat(COLUMN_NAME) from information_schema.COLUMNS where table_name = 'tablename';

 5、查询第一行记录

select  *  from  ‘tablename' limit 1;

6、查询第n行到第m行记录

select * from 'tablename' limit m,n;(n<=m)

7、查询第n行记录

select * from 'tablename' limit n-1,1;

8、查询前n行记录

 -- 方法一
 select * from 'tablename' limit 0,n;
 -- 方法二
 select * from 'tablename' limit n;

9、查询后n行记录

-- 倒序排序,取前n行 id为自增形式
select * from 'tablename' order by id desc dlimit n;

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

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

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

select * from 'tablename' where id<$id  order by id desc dlimit 1
原文地址:https://www.cnblogs.com/soldierback/p/11255852.html