MySQL数据库基本操作--DQL操作

DQL-基本查询

概念

  • 数据库管理系统一个重要的功能就是数据查询,数据查询不应该是简单返回数据库中存储的数据,还应该根据需要对数据进行筛选以及确定数据以什么样的格式显示
  • MySQL提供了功能强大,灵活的语句来实现这些操作
  • MySQL数据库使用select语句来实现查询操作

简化版语法

select 列名 from 表 where 条件

数据准备

create database if not exists mydb2;
use mydb2;
create table product(
pid int primary key auto_increment,
pname varchar(20) not null,
price double,
catagory_id 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,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');

insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,'c004');

简单查询

-- --简单查询
-- --1查询所有商品

select  pid,pname ,price,catagory_id from product;
-- 或者select * from product
-- 

-- 2查询商品名和商品价格
select pname,price from product;

-- 3.1 表别名
select * from product as p;
-- 或者select * from product p;
-- select p.id,u.id from product,user;
-- 3.2 列别名
select pname as '商品名',price '商品价格' from product;
-- 4去掉重复值
select distinct price from product;
-- 或者select distinct * from product;
-- 5查询结果是表达式(运算查询):将所有商品加价十元后显示
select pname,price+10 new_price from product;

运算符

  • 简介

数据库的表结构确定以后,表中数据所代表的意义就已经确定了。通过MySQL运算符进行运算,就可以回去到表结构以外的数据。

例如,学生表中存在一个birth字段,这个字段表示学生的出生年份,那么用现在的时间减去这个字段就可以得到学生的实际年龄数据。

MySQL支持四种运算符

  • 算术运算符
  • 比较运算符
  • 逻辑运算符
  • 位运算符

 算数运算符

+加法运算
-减法运算
*乘法运算
/或DIV除法运算,返回商
%或MOD求余运算,返回余数
算数运算符说明

比较运算符

 逻辑运算符

 位运算符

基本条件查询

-- 查询商品名称为海尔洗衣机的所有商品
 select * from product where pname='海尔洗衣机';
 -- 查询价格为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 >= 200 and price <= 1000;
select * from product where price >= 200 and price <=1000;

select * from product where price between 200 and 1000;
-- 查询商品价格是200或800的所有商品
select * from product where price=200 or price=800;
select * from product where price in (200,800);
-- 查询含有‘裤'字的所有商品
select * from product where pname like '%裤%';
-- 查询以'海'开头的所有商品
select * from product where pname like '海%';-- 百分号表示任意字符
-- 查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%';
-- 查询category_id为null的商品
select * from product where catagory_id is null;
-- 查询category_id不为null分类的商品
select * from product where catagory_id is not null;
-- 使用least求最小值
select least (10,5,20) as small_number;
select least (10,null,20) as small_number;-- 如果求最小、大值时有null,则无法比较,结果之间为null
-- 使用greatest求最大值

 select greatest (10,5,20) as max_number;

运算符操作-算数运算符

-- 1算数运算符
select 6 + 2;
select 6 - 2;
select 6 / 2;
select 6 % 2;

-- 将所有商品加10 元
select pname,price+10 as new_price from product;
-- 将所有商品上调10%
select pname,price * 1.1 as new_price from product;

 位运算符(了解)

排序查询

  • 介绍

如果我们要对读取的数据进行排序,我们就可以使用MySQL的order by子句来设定你想按哪个字段哪种方式进行排序,再返回搜索结果。

select
字段名1,字段名2,...
from 表名
order by 字段名1 [asc|desc],字段名2[asc|desc]
  • 特点

1.asc表示升序,desc代表降序,如果不写默认升序

2.order用于子句中可以支持单个字段,多个字段,表达式,函数,别名

3.order by子句,放在查询语句最后面。LIMIT子句除外。 

操作

-- 1.使用价格排序(降序)
select * from product order by price desc;
-- 2.在价格排序(降序)的基础上,以分类排序(降序)
select * from product order by price desc,category_id asc;
-- 3.显示商品的价格(去重复),并排序(降序)
select distinct price from product order by price desc;

聚合查询

简介

之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个单一的值;另外,聚合函数会忽略空值。

操作

-- 1 查询商品的总条数
select count(*) from product;
-- 2 查询价格大于200商品的总条数
select count(*) from product where price > 200;
-- 3 查询分类为'c001'的所有商品的总和
select sum(price) from product where category_id = 'c001';
-- 4 查询商品的最大价格
select max(price) from product;
-- 5 查询商品的最小价格
select min(price) from product;
-- 6 查询分类为'c002'所有商品的平均价格
select avg(price) from product where category_id = 'c002';

 聚合查询-NULL值的处理

1count函数对null值的处理

如果count函数的参数为星号(*),则统计所有记录的个数。而如果参数为某字段,不统计含null值的记录个数。

2sumavg函数对null值的处理

这两个函数忽略null值的存在,就好象该条记录不存在一样。求平均时也不算个数,如果创建时假设default=0,就算。

3maxmin函数对null值的处理

 maxmin两个函数同样忽略null值的存在。

操作

-- 创建表
create table test_null( 
 c1 varchar(20), 
 c2 int 
);

-- 插入数据
insert into test_null values('aaa',3);
insert into test_null values('bbb',3);
insert into test_null values('ccc',null);
insert into test_null values('ddd',6);
 
-- 测试
select count(*), count(1), count(c2) from test_null;
select sum(c2),max(c2),min(c2),avg(c2) from test_null;

分组查询(很重要)

分组查询是指使用group by字句对查询信息进行分组。

格式

select 字段1,字段2… from 表名 group by 分组字段 having 分组条件;

-- 分组查询
-- select 字段1,字段2...from 表名 group by 分组字段 having 分组条件;
-- 统计各个分类商品的个数,分组之后select后面只能写分组字段和聚合函数
select catagory_id,count(*) from product group by catagory_id;

分组之后的条件筛选-having

分组之后对统计结果进行筛选的话必须使用 having ,不能使用 where
where 子句用来筛选 FROM 子句中指定的操作所产生的行
group  by  子句用来分组 WHERE 子句的输出。
having 子句用来从分组的结果中筛选行
格式

select 字段1,字段2… from 表名 group by 分组字段 having 分组条件;

操作

-- 2.统计各个分类商品的个数,且只显示个数大于4的信息
select category_id ,count(*) from product group by category_id having count(*) > 4;
顺序(from,where,group by,having)

分页查询limit

简介

分页查询在项目开发中常见,由于数据量很大,显示屏长度有限,因此对数据需要采取分页显示方式。例如数据共有30条,每页显示5条,第一页显示1-5条,第二页显示6-10条。 

格式

-- 方式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条开始显示,显示5条 
select * from product limit 3,5
INSERT INTO SELECT语句
格式

insert into Table2(field1,field2,…) select value1,value2,… from Table1 或者:

insert into Table2 select * from Table1

SELECT INTO FROM语句

简介

将一张表的数据导入到另一张表中,有两种选择 SELECT INTO INSERT INTO SELECT

格式

SELECT vale1, value2 into Table2 from Table1

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。

附录 全部代码

create database if not exists mydb2;
use mydb2;
create table product(
pid int primary key auto_increment,
pname varchar(20) not null,
price double,
catagory_id 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,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');

insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,null);

-- --简单查询
-- --1查询所有商品

select  pid,pname ,price,catagory_id from product;
-- 或者select * from product
-- 

-- 2查询商品名和商品价格
select pname,price from product;

-- 3.1 表别名
select * from product as p;
-- 或者select * from product p;
-- select p.id,u.id from product,user;
-- 3.2 列别名
select pname as '商品名',price '商品价格' from product;
-- 4去掉重复值
select distinct price from product;
-- 或者select distinct * from product;
-- 5查询结果是表达式(运算查询):将所有商品加价十元后显示
select pname,price+10 new_price from product;

-- 运算符
use mydb2;
-- 1算数运算符
select 6 + 2;
select 6 - 2;
select 6 / 2;
select 6 % 2;

-- 将所有商品加10 元
select pname,price+10 as new_price from product;
-- 将所有商品上调10%
select pname,price * 1.1 as new_price from product;
-- 2 比较运算符合
 
 -- 3 逻辑运算符
 -- 查询商品名称为海尔洗衣机的所有商品
 select * from product where pname='海尔洗衣机';
 -- 查询价格为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 >= 200 and price <= 1000;
select * from product where price >= 200 and price <=1000;

select * from product where price between 200 and 1000;
-- 查询商品价格是200或800的所有商品
select * from product where price=200 or price=800;
select * from product where price in (200,800);
-- 查询含有‘裤'字的所有商品
select * from product where pname like '%裤%';
-- 查询以'海'开头的所有商品
select * from product where pname like '海%';-- 百分号表示任意字符
-- 查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%';
-- 查询category_id为null的商品
select * from product where catagory_id is null;
-- 查询category_id不为null分类的商品
select * from product where catagory_id is not null;
-- 使用least求最小值
select least (10,5,20) as small_number;
select least (10,null,20) as small_number;-- 如果求最小、大值时有null,则无法比较,结果之间为null
-- 使用greatest求最大值

 select greatest (10,5,20) as max_number;
 -- 4位运算符
-- 使用价格排序(降序)

select * from product order by price desc;
-- 2在价格降序的基础上,以分类降序
select * from product order by price desc,catagory_id desc;
-- 3.显示商品的价格(去重复),并排序(降序)
select distinct price from product order by price desc;

-- 聚合查询

-- 1 查询商品的总条数
select count(pid) from product;
select count(*) from product;
-- 2 查询价格大于200商品的总条数
select count(pid) from product where price > 200;
-- 3 查询分类为'c001'的所有商品的总和
select 
-- 4 查询商品的最大价格
select sum(price) from product where price > 200;
-- 5 查询商品的最小价格
select max(price) from product;
select max(price) max_price, min(price) min_price from product;
-- 6 查询分类为'c002'所有商品的平均价格

select avg(price) from product where catagory_id ='c002';



-- 分组查询
-- select 字段1,字段2...from 表名 group by 分组字段 having 分组条件;
-- 统计各个分类商品的个数,分组之后select后面只能写分组字段和聚合函数
select catagory_id,count(*) from product group by catagory_id;


-- 分页查询
-- 1.查询product表的前五条记录
select * from product limit 5;
-- 2从第四天开始显示,显示五条
 select * from product limit 3,5;-- 从第四天开始显示,显示五条

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值