数据库sql语句

sql:增删改查。curd(c:增加,u:update修改,r:read查询,d:delete删除)

查询:表名和字段名加 ` 可以加快sql语句的查询素的

select username,password from oper_user // 查询oper_user表里的username和password字段
select `username`,`password` from `oper_user` // 查询oper_user表里的username和password字段
select * from oper_user // 查询oper_user里的所有字段

select * from `oper_user` where `id`=2 // 查询oper_user中id为2的数据
select * from `oper_user` where `id`!=2 // 查询oper_user中id不是2的数据
select * from `oper_user` where `id`<>2 // 查询oper_user中id不等于2的数据
select * from `oper_user` where `id`>2 // 查询oper_user中id大于2的数据
select * from `oper_user` where `id`<2 // 查询oper_user中id小于2的数据
select * from `oper_user` where `id`>=2 // 查询oper_user中id大于等于2的数据
select * from `oper_user` where `id`<=2 // 查询oper_user中id小于等于2的数据

select * from `oper_user` where `id` in(1,3,4) // 查询oper_user中id是1、id是3、id是4的数据
select * from `oper_user` where `id` not in(1,3,4) // 查询oper_user中id不是1、id不是3,id不是4的数据
select * from `oper_user` where `id` between 2 and 4 // 查询oper_user中id在2到4之间的数据(包括2和4)
select * from `oper_user` where `username`='admin' and `password`='admin123' // 查询oper_user中username为admin并且password为admin123
select * from `oper_user` where `username`='admin' or `password`='admin123' // 查询oper_user中username为admin或者password为admin123

select * from `oper_user` order by `create_time` asc // 按照create_time 升序
select * from `oper_user` order by `create_time` desc // 按照create_time降序
select * from `oper_user` where `id` in(1,3,4) order by `create_time` desc // 查询id为1,3,4的降序数据
有where where必须要放在order by的前面

select * from `oper_user` where `id` between 4 and 20 limit 0,3 // 第1条开始查,查3条 
limit 起始位置,条数, 第1条数据的起始位置为0

select * from `oper_user` where `id` in(1,6,8,3) order by `id` asc limie 0,4 // id为1,6,8,3的数据根据id升序 第1条开始查,查4条
where在最前面,order by在中间 limit在最后,顺序不能变

统计一张表中总共有多少数据
select count(*) from `oper_user` // 统计oper_user表中共有多少条数据
select count(*) as allnum from `oper_user` // as 取别名

select max(`id`) from `oper_user` // 查询表中最大的id值
select min(`id`) from `oper_user` // 查询表中最小的id值
select avg(`id`) from `oper_user` // 查询表中id的平均值
select sum(`id`) from `oper_user` // 表中所有id的和

增加:

insert into 表名 (字段1,字段2,字段3) values(字段1的值,字段2的值,字段3的值) // 

修改:

update 表名 set 字段名=字段值
update 表名 set 字段1=字段1的值,字段2=字段2的值 where 条件
update `oper_user` set `username`='admin',`password`='admin123' where `id`=2

删除:

delete from 表名 where 条件
delete from `oper_user` where `id`=1
展开阅读全文

页面更新:2024-05-20

标签:语句   升序   表里   平均值   别名   字段   条件   位置   数据库   降序   数据

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top