DQL 查询语句
时间: 2019-06-20来源:OSCHINA
前景提要
【围观】麒麟芯片遭打压成绝版,华为亿元投入又砸向了哪里?>>>
语法总结 :
顺序在最后

" * " 就相当于 一整排的意思
insert into 是写入这行 如果遇到非空 必须赋值 哪怕是 null
alter 是改变的意思 用于结构的增加列 一般和 alter table.... add ...
update 是更新数据的意思 用于给 字段 更改值 update ....set....
想显示两个 就在 select后面 两个字段并且用个 , 隔开 如 : pid,pname
想看一组的数据 就先引用 alter add 加上cid列 再 用update set给列分开 最后 用group by 查这组的值


4.1 数据准备 # 创建商品表 create table product( pid int primary key auto_increment, pname varchar(20), price double, pdate timestamp ) # 自动增长列:auto_increment,要求:1,必须整型(int) 2.必须是主键 insert into product values (null,' 谭妮平',0.01,null); insert into product values (null,' 李士雪',38,null); insert into product values (null,' 左慈',-998,null); insert into product values (null,' 黄迎',99999,null); insert into product values (null,' 南国强',99998,null); insert into product values (null,' 士兵',1,null); insert into product values (null,' 李士兵',698,null); 4.2 简单查询(重点前3个) 查询的语法: s elect 要查询的字段 from 表名 where 条件; 1. 查询 所有 商品 s elect * from 表名 ; select * from product; 2. 查询商品名和商品价格 s elect 要查询的字段(多个字段用逗号隔开) from 表名; select pname,price from product; 3. 查询所有商品信息使用表别名
【单表没啥卵用,主要用于多表操作!】 s elect * from 表名 别名; select * from product p; 4. 查询商品名,使用列别名(相对表别名使用要少一些。) select 列名 列别名 from 表名; 4.3 条件查询 语法:select * from 表名 where 条件 1. 查询商品名称为"左慈"的商品信息 - --- 查一行 s elect * from product where pname=’ 左慈 ’ ; 2. 查询价格>60元的所有商品信息 3. 查询商品名称含有"士"字的商品信息【 模糊查询 】 select * from 表名 where 字段名 like ‘% 士% ’ ; 【%表示任意个字符】
查询商品名称以"士"字开头的商品信息 select * from 表名 where 字段名 like ‘ 士% ’; 【%表示任意个字符】
查询商品名称第二字为"士"字的商品信息 select * from 表名 where 字段名 like ‘_ 士% ’; 【_表示1个字符】 4. 查询商品id在(3,6,9)范围内的所有商品信息【关键字 in 】 s elect * from product where pid in(3,6,9); and or not 5. 查询商品名称为士兵 并且 价格>0的商品信息 说明:并且( and ) 6. 查询商品名称含有士字 或者 价格>100的商品信息 7.查询 id 不是2 的商品信息 ----- not (pid = 2); != 也行 8. 查询价格在100到10000之间的商品信息 between A and B (前后闭区间 都包) 4.4 排序 排序语法:select * from 表名 order by 指定的 字段名称 [ asc 升序]/desc降序 查询所有的商品,按价格进行排序(升序、降序) 升序: select * from product order by price asc;
或者 select * from product order by price;
降序: select * from product order by price asc; 1.2. 查询名称有"士"的商品信息并且按照价格降序排序 select * from product where pname like '% 士%' order by price desc; 注意:order by排序操作必须放在where 条件的后面!!! 4.5 聚合 之前的查询操作都是横向操作(以行为单位进行查询),使用聚合函数,可以进行纵向操作(以列为单位进行查询,查询的结果是单独的一列) 就和Excel一样 语法:select 函数名称(给定列名) from 表名 2.1. 获得所有商品的价格的总和 sum s elect sum(price) from product; 2.2. 获得所有商品的平均价格 avg se lect avg(price) from product; 2.3. 获得所有商品的个数 count s elect count( * ) from product; 4.6 分组 语法:select * from 表名 group by 分组的字段名称 这 个只在把一组当成一个整体的时候才用,分组是用的加列的思想; 1. 添加分类id (alter table product add cid varchar(32);) - -加上cid这个列而已 2. 初始化数据 --- 给这个列赋值 update product set cid='1'; update product set cid='2' where pid in (5,6,7);
查询: 3.1. 根据cid字段分组,分组后统计商品的个数。 --- 统计每组的 select cid,count(cid) from product group by cid; 3.2. 根据cid分组,分组统计每组商品的平均价格,并且平均价格大于20000元。 select cid,avg(price) from product group by cid having avg(price)>20000;
如果进行了分组操作,还带有条件,不能使用where关键字,必须使用having,而且只能放在最后面!!! 4.7 分页 查询 LIMIT
select * from product limit 2; 查前两个
select pname from product limit 4,2; 公式 : (要看的页码数 - 1)*每页显示大小
4.9 删除 delete from 表名 全删 delete from 表名 where 条件 delete from product where pid=8; 4.8 单表查询总结 顺序 开发中关于查询语句应该怎么去写 ? 【关键字出现的顺序如下:】 select 被查询的字段(所有的字段写 * )【一般都是 的 字后面的内容】 from 表名 where 条件 【一般都是 的 字前面的内容】 grou p by 分组的字段名称 【一般会明确指定根据什么来分组】 order by 升序还是降序( asc/desc ) 【一般会明确指定根据什么来分组】 having 条件(分组后的条件) 【一般会明确指定根据什么来分组】


科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行