简单的sql语句
查
语法
select 要查询的字段... from 表名
作用
查询某个表中的某个或几个字段
例
select id, username from student -- 查询student表中的所有人的id和username
查询所有字段 用*
select * from student
带条件的查询
select 要查询的字段... from 表名 while 条件 //多个条件可以and
例
-- 查询所有男同学的名字
select username from student while sex='男'
-- 查询大于20岁男同学的信息
select * from student while sex='男' and age>20
排序
语法
select 字段,字段2...form 表名 [where条件] order by 字段 排序规则
例
select * from student order by age asc -- 根据年龄age升序
select * from student order by age desc -- 根据年龄age降序
查询指定数量limit
语法
select 字段, 字段, … from 表名 limit 开始索引,查询条数
例
select * from student limit 1,5 -- 从1 索引开始,查询5条同学表数据,常用语分页查询
select * from student order by age desc limit 0,3 -- 查询年龄最大的3位同学
统计count()
语法
select count(表主键) from 表名
例子
select count(id) from student -- 统计学生人数
select count(id) from student where sex='男' -- 统计男同学人数
增
语法
insert into 表名(字段1,字段2...) values('值1','值2'...)
例
insert into student(name,age,sex) values("张三",13,'男') -- 添加新同学
改
语法
update 表名 set 字段=值,字段2=值... [where条件]
例
update student set age=age+1 -- 所有人年龄加1
update student set username='李思思' where id=3 -- 将id为3的同学名字更改为李思思
删
语法
delete from 表名 [where 条件]
有条件删除
delete from student where sex='男' -- 删除男学生
无条件删全部
delete from student -- 删除全部学生
删除表
drop table 表名 -- 把整张表毁灭,不留尸体
truncate table 表名 -- 清除表的内容,留下尸体
delete from 表名 -- 一行一行清除表的内容,效率慢,通常带条件删除
删库
drop 库名 -- 数据无价,慎重操作