0
点赞
收藏
分享

微信扫一扫

数据库学习-数据库基本操作DQL-基本查询

蚁族的乐土 2022-01-26 阅读 56

数据库管理系统一个重要功能就是数据查询,数据查询不应只是简单返回数据库中存储的数据,还应该根据需要对数据进行筛选以及确定数据以什么样的格式显示。

MySQL提供了功能强大、灵活的语句来实现这些操作。

MySQL数据库使用select语句来查询数据。

语法格式

select 
    [all|distinct]
    目标列的表达式1 别名,
    目标列的表达式2 别名...
from 表名或视图名 别名,表名或视图名 别名...
[where 条件表达式]
[group by 列名]
[having 条件表达式]
[order by 列名 [asc|deac]]
[limit 数字或列表];

简化版语法

select *| 列名 from 表 where 条件;


数据准备

-- 1.创建数据库
create database if not exists mydb2;
use mydb2;
-- 2.创建商品表
create table product(
pid int primary key auto_increment,-- 商品编号
pname varchar(20) not null,-- 商品名字
price double,-- 商品价格
category_id varchar(20)-- 商品所属分类
);
-- 3.添加数据
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,'花花公子夹克',400,'c002');
insert into product values(null,'劲霸休闲裤',530,'c002');
insert into product values(null,'海澜之家卫衣',580,'c002');
insert into product values(null,'杰克琼斯运动裤',480,'c002');

insert into product values(null,'兰蔻面霜',600,'c003');
insert into product values(null,'雅诗兰黛精华水',680,'c003');
insert into product values(null,'香奈儿香水',360,'c003');
insert into product values(null,'SK-Ⅱ神仙水',580,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');

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


简单查询

查询所有的商品

select * from product;

查询商品名和商品价格

select pname,price from product;

别名查询 使用的关键字是as(as可以省略)

-- 表别名
select * from product as p;
select * from product p;
-- 列别名
select pname,price from product;
select pname as '商品名',price '商品价格' from product;

去掉重复值

select distinct price from product;
select distinct * from product;-- 去除所有列的重复值

查询结果是表达式(运算查询)

select pname,price+10 new_price from product;

运算符

数据库中的表结构确立后,表中的数据代表的意义就已经确定。通过MySQL运算符进行运算,就可以获取到表结构以外的另一种数据。

MySQL支持4种运算符:

算数运算符

比较运算符

逻辑运算符

位运算符

 位运算符是在二进制数上进行计算的运算符。位运算会先将操作数变成二进制,进行位运算。然后再将计算结果从二进制数变回十进制数。

运算符操作-算数运算符

select 6+2;
select 6-2;
select 6*2;
select 6/2;
select 6%2;
select pname,price+10 as new_price from product;
select pname,price*1.1 as new_price from product;

运算符操作-条件运算符

-- 查询商品名称为海尔洗衣机的商品所有信息
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);
-- 查询商品价格大于等于80元的所有商品信息
select * from product where price>=80;
-- 查询商品价格在200到1000之间的所有商品
select * from product where price>=200 and price<=1000;
select * from product where price>=200 && price<=1000;
select * from product where price between 200 and 1000;
-- 查询商品价格是200或800的所有商品
select * from product where price in(200,800);
select * from product where price=200 or price=800;
select * from product where price=200 || price=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 category_id is null;
-- 查询category_id不为null的商品
select * from product where category_id is not null;
-- 使用least求最小值
select least(10,5,30) as small_number;
select least(10,null,30);-- 如果求最小值时,有值为null,则不会进行比较,结果直接为null
-- 使用greatest求最大值
select greatest(10,20,30) as big_number;
-- 如果求最大值时,有值为null,则不会进行比较,结果直接为null

运算符操作-位运算符(了解)

select 3&5;-- 位与
select 3|5;-- 位或
select 3^5;-- 位异或
select 3>>1;-- 位右移
select 3<<1;-- 位左移
select ~3;-- 位取反

排序查询

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

select
字段1,字段2,...
from 表名
order by 字段1 asc|desc,字段2 asc|desc...

特点

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

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

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

使用价格降序排序

select * from product order by price desc;

在价格降序的基础上,以分类降序排序

select * from product order by price desc,category_id desc;

显示商品的价格(去重)并降序排序 

select distinct price from product order by price desc;

聚合查询

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

 查询商品的总条数

select count(pid) from product;
select count(*) from product;

查询价格大于200商品的总条数

select count(pid) from product where price>200;

查询分类为'c001'的所有商品总和

select sum(price) from product where category_id='c001';

查询商品的最大价格

select max(price) from product;

查询商品的最小价格

select min(price) from product;

查询分类为'c002'所有商品的平均价格

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

聚合查询-NULL值的处理

1、count函数对null值的处理

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

2、sum和avg函数对null值的处理

这两个函数忽略null值的存在,就好像该条记录不存在一样。

3、max和min函数对null值的处理

max和民两个函数同样忽略null值的存在。

举报

相关推荐

0 条评论