Mysql 原生 SQL 语句查询

239 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1、查询表里的所有数据

select * from admin

2、根据字段条件查询

select * from admin  where  id =1  

3、模糊查询

select * from admin where 字段 like %name%; #前后匹配

select * from admin where 字段 like 值%; #后匹配

select * from admin where 字段 like #值; #前匹配

4、增加

insert into admin (  'name','pwd','status') values ('admin'.12346,1);

5、删除

delete from admin where id=1

6、修改

update admin set name='root' where id=1;

7、排序

#asc 正序   desc 倒序

select * from admin where id >1 order by id asc;

指定字段字符串排序

SELECT * FROM admin ORDER BY field(status, 101, 106) desc,id desc

8、指定条件查询

select * from admin where id limit 10,15;   #根据id从第10条开始查询,查询15条

select * from admin where id in (1,2); //查询id为1和2的数据

9、指定范围查询

select * from admin where id between 10 and  15; #从id 10到 15之间的数据,包括10、15

10、查询某个字段的平均值

select avg('num') from admin;

11、字段求和

select sum('price) from admin;

12、查询某个字段值最大、最小的数据

select max('price') from admin; #最大

select min('price') from admin; #最小

13、查询表里数据的总条数

select count() from admin; 

#数据多的话 建议用下面的

select count('id') from admin;