SQL 语句(原生)


//查

 

//查询表里的所有数据

select * from 表名   

 

//根据id等字段查询数据

select * from 表名 where 字段 = 值 or 字段 = 值 

(例):select * from 表名 where id = 1 or name = admin;

 

//模糊查询

like % 值%

(例):select * from 表名 where 字段 link %值%; //前后匹配

 

//随机查询一条数据

select * from 表名 order by rand() LIMIT 1

 

//增

 

insert into 表名 (字段1,字段2) values (值1,值2);

(例):insert into 表名 ('id','name') values ('','admin');

 

//删

 

delete from 表名 where 字段=值;

(例):delete from 表名 where id=1;

 

delete from 表名 where id in (1,2,3);//批量删除

 

truncate TABLE   表名;//清空表里的所有数据

 

//改

 

update 表名 set 字段=值,字段=值 where 字段=值;

(例):update 表名 set name='1' where id=1;

 

 

//杂

 

//排序

asc  正序

desc 倒序

(例):select * from 表名 where id >1 order by id asc;  //id大于1的从小到大排序

 

 

//指定位置查询

limit 

(例):select * from 表名 where id limit 2,4;//根据id从第2条开始查询,查询4条

 

 

//指定条件查询

in

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

 

 

//指定范围查询

beteeen ...and...

(例):select * from 表名 where id between 3 and 5; //从id三到五之间的数据,包括三五

 

//查询某个字段的平均值

(例):select avg(字段名) from 表名;

 

//某个字段求和

(例):select sum(字段名) from 表名;

 

//查询某个字段值最大的数据

(例):select max(字段名) from 表名;

 

//查询某个字段值最小的数据

(例):select min(字段名) from 表名;

 

//查询表里数据的总条数

(例):select count(字段名) from 表名;

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM