当前位置: 首页 > article >正文

mysql基础之DQL基本单表查询

学习DQL之前先知道sql语句的执行顺序
from->join->on->where->group by->count(字段)->having->select->distinct->order by->limit

  1. null无法和任何值进行比较(不相等),包括null和null也不相等

1.DQL简单查询

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

1.1语法格式

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

简化版语法

select *| 列名 fromwhere 条件

1.2. 数据准备

  1. 创建数据库和表
-- 创建数据库
create database if not exists mydb2;
use mydb2;
-- 创建商品表:
create table product(
 pid int primary key auto_increment, -- 商品编号
 pname varchar(20) not null , -- 商品名字
 price double,  -- 商品价格
 category_id varchar(20) -- 商品所属分类
);
  1. 添加数据
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,'九阳电饭煲',200,'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. 简单查询
-- 1.查询所有的商品.  
select *  from product;
-- 2.查询商品名和商品价格. 
select pname,price from product;
-- 3.别名查询.使用的关键字是as(as可以省略的).  
-- 3.1表别名: 
select * from product as p;
-- 3.2列别名:
select pname as pn from product; 
-- 4.去掉重复值.  
select distinct price from product;
-- 5.查询结果是表达式(运算查询):将所有商品的价格+10元进行显示.
select pname,price+10 from product;

2.DQL语句中运算符操作

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

例如,学生表中存在一个birth字段,这个字段表示学生的出生年份。而运用MySQL的算术运算符用当前的年份减学生出生的年份,那么得到的就是这个学生的实际年龄数据。
MySQL支持4种运算符

  1. 算术运算符
    在这里插入图片描述
    案例
select 6 + 2;
select 6 - 2;
select 6 * 2;
select 6 / 2;
select 6 % 2;
 
-- 将每件商品的价格加10
select name,price + 10 as new_price from product;
-- 将所有商品的价格上调10%
select pname,price * 1.1 as new_price from product;

  1. 比较运算符
    在这里插入图片描述
    案例
-- 查询商品名称为“海尔洗衣机”的商品所有信息:
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 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 category_id is null;
 
-- 查询category_id不为null分类的商品
select * from product where category_id is not null;

-- 使用least求最小值
select least(10, 20, 30); -- 10
--如果求最小值时,有个值为null,则不会进行比较,结果直接为nu1l;
select least(10, null , 30); -- null
 
-- 使用greatest求最大值
select greatest(10, 20, 30);
-- --如果求最大值时,有个值为null,则不会进行比较,结果直接为nu1l;
select greatest(10, null, 30); -- null
  1. 逻辑运算符
    在这里插入图片描述

  2. 位运算符
    在这里插入图片描述
    位运算符是在二进制数上进行计算的运算符。位运算会先将操作数变成二进制数,进行位运算。然后再将计算结果从二进制数变回十进制数。

select 3&5; -- 位与  都为1则为1
select 3|5; -- 位或  一个为1则为1
select 3^5; -- 位异或  不同则为1相同则为0
select 3>>1; -- 位左移
select 3<<1; -- 位右移
select ~3;   -- 位取反

3.排序查询 order by

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

select 
 字段名1,字段名2,……
from 表名
order by 字段名1 [asc|desc],字段名2[asc|desc]……

3. 特点
①asc代表升序,desc代表降序,如果不写默认升序
②order by用于子句中可以支持单个字段,多个字段,表达式,函数,别名
③order by子句,放在查询语句的最后面。LIMIT子句除外

④如果order by后边跟一个字段,则只会按照该字段的值进行排序,该字段必须为数值类型或者英文和数字字符串类型(‘beijing’)或者“2020-12-23”
⑤ 如果order by后边跟多个字段:order by c1,c2
解释:先安装c1来排序,如果c1相同,则按照c2来排
4. 操作

-- 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;

第二条sql语句结果如下优先按照price降序,当价格相等时按照category_id升序
在这里插入图片描述

4.聚合查询

4.1聚合函数

1. 简介
之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个单一的值;另外聚合函数会忽略空值。
2. 函数
count()查看某列或者某张表的行数
在这里插入图片描述
注意:
count(pid) 如果列有null值则不会进行统计
sum(price),max(price),min(price),avg(price) 如果列有null值则不会进行统计,当作不存在,列必须为数值列
3. 操作

-- 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';

聚合函数一般会和分组一起使用

4.2 聚合函数,NULL值的处理

1. 介绍
1、count函数对null值的处理
如果count函数的参数为星号(*),则统计所有记录的个数。而如果参数为某字段,不统计含null值的记录个数。

2、sum和avg函数对null值的处理
这两个函数忽略null值的存在,就好象该条记录不存在一样。

3、max和min函数对null值的处理
max和min两个函数同样忽略null值的存在。
2. 操作

-- 创建表
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; -- count(1)等价于count(*)
select sum(c2),max(c2),min(c2),avg(c2) from test_null;

5.分组查询 group by 和having

5.1 group by 分组

1. 简介
分组查询是指使用group by字句对查询信息进行分组。
2. 格式:

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

3. 操作

-- 1 统计各个分类商品的个数
select category_id ,count(*) from product group by category_id ;

4. 注意点

  1. 分组可以理解为将一张表临时拆分成多张表,拆分的依据就是分组字段

  2. 如果要进行分组的话,则SELECT子句之后,只能出现分组的字段和聚合函数,其他的字段不能出现:

  3. 分组可以根据一个字段,也可以根据多个字段,如果是一个字段则该字段相同就会分到同一组,如果是多个字段,则多个字段都相同才能分到同一组

    如果group by后面跟了多个字段如下,则必须是同一个省同一个市同一个县才可以分到一组,省市县可以不区分先后顺序
    在这里插入图片描述

5.2 分组之后的条件筛选-having

1. 简介

  1. 分组之后对统计结果进行筛选的话必须使用having,不能使用where
  2. where子句用来筛选 FROM 子句中指定的操作所产生的行
  3. group by 子句用来分组 WHERE 子句的输出。
  4. having 子句用来从分组的结果中筛选行
    2. 格式
select 字段1,字段2from 表名 group by 分组字段 having 分组条件;

3. 操作

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

在这里插入图片描述
上述sql语句的执行书讯
在这里插入图片描述

6.分页查询-limit

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

-- 方式1-显示前n条
select 字段1,字段2... from 表明 limit n
-- 方式2-分页显示
select 字段1,字段2... from 表明 limit m,n
m: 整数,表示从第几条索引开始,计算方式 (当前页-1*每页显示条数
n: 整数,表示查询多少条数据

3. 操作

-- 查询product表的前5条记录 
select * from product limit 5 

-- 从第4条开始显示,显示5条 
select * from product limit 3,5

7. 将一张表的数据导入到另一张表中

7.1INSERT INTO SELECT语句

1. 简介
将一张表的数据导入到另一张表中,可以使用INSERT INTO SELECT语句 。
2. 格式

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

要求目标表Table2必须存在

7.2 SELECT INTO FROM语句

1. 简介
将一张表的数据导入到另一张表中,有两种选择 SELECT INTO 和 INSERT INTO SELECT 。
2. 格式

SELECT vale1, value2 into Table2 from Table1

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


http://www.kler.cn/news/160181.html

相关文章:

  • 『Jmeter超级干货』| Linux下Jmeter安装配置、脚本设计执行、监控及报告完整过程
  • Windows 下 PyTorch 入门深度学习环境安装与配置 GPU 版
  • Windows server 部署iSCSI共享磁盘搭建故障转移群集
  • BearPi Std 板从入门到放弃 - 引气入体篇(9)(DAC->ADC)
  • Java LeetCode篇-深入了解二叉树经典解法(三种方式实现:获取二叉树的最大深度)
  • Redis——某马点评day03——part2:秒杀业务异步优化
  • 鸿蒙4.0开发笔记之ArkTS语法基础之应用生命周期与页面中组件的生命周期(十六)
  • Park Unpark
  • Web安全漏洞分析-XSS(下)
  • ApplicationContextAware 类
  • ELK 日志解决方案
  • AI网关究竟是什么,怎么样才算是AI算力的网关
  • 跟着GPT学习shell脚本,理论与实践相结合的学习计划。(一)
  • 团队git操作流程
  • 单片机开发常用的软件构架
  • html5各行各业官网模板源码下载(1)
  • 19、pytest通过mark标记测试函数
  • 每天一点python——day85
  • 记录一次vscode markdown的图片路径相关插件学习配置过程
  • 【微服务】分布式限流如何实现
  • Android10 Dialog bug
  • 【技术干货】宇视IPC音频问题解决步骤
  • 编程常见的问题
  • Java动态代理实现与原理详细分析
  • linux环境下编译安装OpenCV For Java(CentOS 7)
  • 判断完数(写出部分函数)
  • Uncle Maker: (Time)Stamping Out The Competition in Ethereum
  • Ribbon 饥饿加载
  • Fabric:创建应用通道
  • 图的建立与实现(使用邻接矩阵)(附赠Kruskal算法)