一 数据准备
创建数据库和商品表
create database if not exists mydb2;
use mydb2;
create table product(
pid int primary key auto_increment,
name varchar(20) not null,
price double,
categoryid varchar(20)
);
添加数据
insert into product values(null,'海尔洗衣机',5000,'c001') ;
insert into product values(null,'美的冰箱',3000,'c001') ;
insert into product values(null,'格力空调',5000,'c001') ;
insert into product values(null,'九阳电饭煲',5000,'c001') ;
insert into product values(null,'衬衣',300,'c002') ;
insert into product values(null,'西裤',800,'c002') ;
insert into product values(null,'夹克',440,'c002') ;
insert into product values(null,'休闲裤',266,'c002') ;
insert into product values(null,'卫衣',180,'c002') ;
insert into product values(null,'运动裤',430,'c002') ;
insert into product values(null,'面霜',300,'c003') ;
insert into product values(null,'精华水',200,'c003') ;
insert into product values(null,'香水',340,'c003') ;
insert into product values(null,'神仙水',366,'c003') ;
insert into product values(null,'粉底',180,'c003') ;
insert into product values(null,'方便面',43,'c004') ;
insert into product values(null,'海带丝',18,'c004') ;
insert into product values(null,'坚果',88,null) ;
二 简单查询
1,查询所有商品
select * from product;
2,查询商品名和商品价格
select name,price from product;
3,别名查询使用关键字as(as可以省略)
表别名
select * from product as p;
select * from product p;
列别名
select name as pn from product;
4,去掉重复值
select distinct price from product;
5,查询结果是表达式:将所有商品价格+10显示
select name,price+10 from product;
三 运算符
数据库中的表结构确立后,表中数据代表的意义就已经确定,通过MySQL运算符进行运算,就可以获取到表结构以外的另一种数据。MySQL支持四种运算符,算数运算符,比较运算符,逻辑运算符,位运算符。
算术运算符 |
说明 |
+ |
加法运算 |
- |
减法运算 |
* |
乘法运算 |
/ 或 DIV |
除法运算,返回商 |
% 或 MOD |
求余运算,返回余数 |
比较运算符 |
说明 |
= |
等于 |
< 和 <= |
小于和小于等于 |
> 和 >= |
大于和大于等于 |
<=> |
安全的等于,两个操作码均为NULL时,其所得值为1;而当一个操作码为NULL时,其所得值为0 |
<> 或!= |
不等于 |
IS NULL 或 ISNULL |
判断一个值是否为 NULL |
IS NOT NULL |
判断一个值是否不为 NULL |
LEAST |
当有两个或多个参数时,返回最小值 |
GREATEST |
当有两个或多个参数时,返回最大值 |
BETWEEN AND |
判断一个值是否落在两个值之间 |
IN |
判断一个值是IN列表中的任意一个值 |
NOT IN |
判断一个值不是IN列表中的任意一个值 |
LIKE |
通配符匹配 |
REGEXP |
正则表达式匹配 |
逻辑运算符 |
说明 |
NOT 或者 ! |
逻辑非 |
AND 或者 && |
逻辑与 |
OR 或者 || |
逻辑或 |
XOR |
逻辑异或 |
位运算符 |
说明 |
| |
按位或 |
& |
按位与 |
^ |
按位异或 |
<< |
按位左移 |
>> |
按位右移 |
~ |
按位取反,反转所有比特 |
运算符操作
将所有商品价格上调10%
select name,price*1.1 newp from product;
查询商品海尔洗衣机的信息
select * from product where name = '海尔洗衣机';
查询价格为800的商品信息
select * from product where price = 800;
查询价格不为800的商品信息
select * from product where price != 800;
select * from product where price <> 800;
select * from product where not(price = 800);
查询价格大于60的商品信息
select * from product where price >= 60;
查询价格在200到1000之间的商品信息
select * from product where price between 200 and 1000;
select * from product where price >= 200 and price <=1000;
select * from product where price >= 200 && price <=1000;
查询价格是200或800的商品信息
select * from product where price in(200,800);
select * from product where price = 200 or price =800;
查询含有”裤“字的所有商品
select * from product where name like '%裤%';# %用来匹配任意字符
查询“西”开头的所有商品
select * from product where name like '西%';# %用来匹配任意字符
查询第二个字为“衣”的所有商品
select * from product where name like '_衣%';# _用来匹配单个字符
查询categoryid 为/不为 空的商品信息
select * from product where categoryid is null;
select * from product where categoryid is not null;
使用least求最小值,使用greatest求最大值
select least(50,null,60);# 求最小值,如果有null,结果直接为null
select greatest(50,null,60);# 求最大值,如果有null,结果直接为null
四 排序查询
如果我们要对读取的数据进行排序,我们要用MySQL的order by子句来设定按哪种方式进行排序,再返回搜索结果: select 字段名1,字段名2.... from 表名 order by 字段名1 [asc|desc],字段名2 [asc|desc]....
asc代表升序,desc代表降序,如果不写默认升序。order by 用于子句中可以支持单个字段,多个字段,表达式,函数,别名。order by 子句放在查询语句最后,limit子句除外。
将所有商品按价格降序
select * from product order by price desc;
在价格降序基础上,以分类降序
select * from product order by price desc,categoryid desc;#先按主要条件排序,主要条件相同时按次要条件排序
显示商品价格去重复,并降序排列
select distinct price from product order by price desc;
五 聚合查询
聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个单一的值,另外聚合函数会忽略空值
聚合函数 |
作用 |
count() |
统计指定列不为NULL的记录行数; |
sum() |
计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0 |
max() |
计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算; |
min() |
计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算; |
avg() |
计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为0 |
查询商品总条数
select count(*) from product;
查询价格大于200的商品总条数
select count(pid) from product where price > 200;
查询分类为‘c001’的所有商品价格总和
select sum(price) from product where categoryid='c001';
查询商品最大价格,最小价格
select max(price) from product;
select min(price) from product;
查询分类为‘c002’所有商品的平均价格
select avg(price) from product where categoryid='c002';
六 分组查询
分组查询是使用group by 子句对查询信息进行分组:select 字段1,字段2… from 表名 group by 分组字段 having 分组条件; 如果要分组,则select子句后只能出现分组的字段和统计函数,其他字段不能出现。
统计各个分类商品的个数
select categoryid,count(pid) from product group by categoryid;
分组之后的条件筛选用having,不能使用where,where子句用来筛选from子句中指定的操作所产生的行,group by 子句用来分组where子句大的输出,having子句从分组的结果中筛选行。
格式:select 字段1,字段2… from 表名 group by 分组字段 having 分组条件;
统计各个分类商品的总数,且只显示个数大于4的信息
select categoryid,count(pid) cnt from product group by categoryid having cnt>4
七 分页查询
分组查询在项目开发中很常见,由于数据量很大,显示屏长度有限,因此要对数据采取分页显示的方式。
-- 方式1-显示前n条 :select 字段1,字段2... from 表明 limit n
-- 方式2-分页显示 :select 字段1,字段2... from 表明 limit m,n
m: 整数,表示从第几条索引开始,计算方式 (当前页-1)*每页显示条数
n: 整数,表示查询多少条数据
查询product前5条记录
select * from product limit 5;
从第4条开始显示,显示五条
select * from product limit 3,5;# 第一条为0
SQL执行先后顺序 from,where,group by,count,having,select,order by,limit。