1.插入记录
insert into 表( 列) values ( 值) ; 按照列插入值
例:insert into table ( pname, price) values ( 'mac' , 20000 ) ;
insert into 表 values 值
例:insert into table values ( null , '苹果电脑' , 20000 ) ;
2.更新记录
update 表 set 列= 值, 列= 值 where 条件
例
update product
3.删除记录
delet from 表 where 条件
例:delete from producr where price = 20000 ;
4.查询记录[重点]
1. select * from 表
2. 查询商品表里面的所有的列
select * from product;
3. 查询商品名字和价格
select name, price from product;
4. 去重查询商品的价格
select distinct price from product ;
5. 取出表中的每条数据,满足条件的记录就返回,不满足条件的记录不返回
select . . . from 表 where pid in ( 1 , 2 , 3 , 4 )
6. between . . . and . . . 区间查询
select * from product where price between 1000 and 2000 相当于1000 <= price<= 3000
7. like 模糊查询 一般和_或者% 一起使用
- _ 占一位
- % 占0 或者n位
select * from table where name like '张%'
8. and 多条件同时满足
where 条件1 and 条件2 and 条件3
9. 任意条件满足
where 条件1 or 条件2 or 条件3
5.排序查询
1. 语法: 只按某一个字段进行排序,单列排序
select . . . from 表明 where 条件 order by 字段名[ asc | desc ] asc :升序, desc :降序, 不指定默认升序
2. 练习: 以分数降序查询所有的学生
select * from student order by score desc
6.聚合函数
1. 最大值 max ( 列名)
select max ( score) from student
2. 最小值 min ( 列名)
3. 平均值 avg ( 列名)
4. 求和 sum ( 列名)
7.分页查询
1. select . . . from . . . . limit 起始行数, 查询的记录条数.
limit a, b
a 起始行
b 返回的行数
例:每页查询4 个
第一页 a= 0 , b= 4
第二页 a= 4 , b= 4
第三页 a= 8 , b= 4