数据库基本操作:
- select 要查询的列 from 表名 where 查询条件
- select语句:查询
select name from product
单独检索产品表中产品名称这一列
select * from product
检索产品表中所有列
限制结果:
- distinct语句:去重
select distinct name from product
检索产品表中所有产品名称,并且去掉名称重复的数据
- limit语句:限制读取n条记录
select name from product limit 4;
读取产品表中的产品名称,且只取前4条记录
select name from product limit 2,5;
读取产品表中的产品名称,且只从第3条记录开始读取,读取5条记录
- 表名.列名字:限制使用字段的表名
数据库名.表名.字段名:同理,指定数据库,指定表,指定字段
select produt.id from product
product.id,表示读取产品表中的id列
- order by 字段:针对某个字段排序
- order by字段:针对某些字段排序,字段位置很重要,优先根据靠前的字段排序
select id,name from product order by name;
select id,name from product order by id;
--可针对id 或者名称都可以排序
select id,name from product order by name,id;
优先根据产品名称进行排序,若名称相同,则根据id排序
- asce:按照升序排序(可不写,默认为升序)
- desc:按照降序排序
select id,name from product order by name desc,id asce;
在产品列表中,将现将名字按照降序排序,后id按照升序排序(先按照名称排序,名称排序相同时,在按照id的升序排序)