Mysql的DQL查询操作全面分析讲解
作者:coleak 发布时间:2024-01-18 02:07:27
标签:Mysql,DQL,查询
DQL简介
概念:DQL(data query language)数据查询语言 select操作
排序规则:
- select 表达式1|字段,.... - from 表名 where 条件 - group by 列名 - having 条件 - order by 列名 asc|desc - limit 位置,数量
语法结构:
SELECT [ALL | DISTINCT] ALL表示查询出所有的内容 DISTINCT 去重
{* | 表名.* | 表名.字段名[ AS 别名][,...]} 指定查询出的字段的
FROM
表名[AS 别名][,表1... AS 别名]
[INNER | [LEFT | RIGHT] [OUTER] JOIN 另一张表名 [AS 别名] ON 关联条件]
[WHERE 条件]
[GROUP BY 分组字段[,...]]
[HAVING 给分组后的数据进行条件筛选]
[ORDER BY 排序字段[,...]]
[LIMIT [startIndex,]pageSize]
具体操作
数据准备
create database if not exists test;
use test;
create table if not exists data(
id tinyint primary key auto_increment,
price double NOT null,
name varchar(20) not null,
type varchar(20) not null)
;
insert into data values
(null,900,'洗衣机','b'),
(null,1900,'冰箱','b'),
(null,2900,'空调','b'),
(null,3900,'电视','b'),
(null,150,'衣服','c'),
(null,180,'裤子','c'),
(null,200,'鞋子','c'),
(null,188,'洗面奶','a'),
(null,188,'洗发水','a'),
(null,199,'洗衣液','a'),
(null,88,'沐浴露','a'),
(null,5,'泡面','d'),
(null,15,'饼干','d'),
(null,30,'咖啡','d');
简单查询
select * from data;
select name,price from data;
select * from data as d;
select * from data d;
select d.name,d.price from data d;
select distinct price from data;
select name,price +100 newprice from data;
运算符
算术运算符
select name,price *1.5 newprice from data;
条件查询
select * from data where name='洗衣机';
select * from data where !(price>100);
select * from data where price between 200 and 1000;
select * from data where price in(188,900);
-- 等于下面两句
select * from data where price = 188 or price =900;
select * from data where price = 188 || price =900;
select * from data where name like '%衣%';
select * from data where name like '衣%';
select * from data where name like '_衣%';
select * from data where id is null;
注释:当有NULL作为比较大小的对象时,最大值和最小值均为null
排序查询
select * from data order by price;
select * from data order by price desc;
select distinct price from data order by price desc;
select * from data order by price,id;
聚合查询
select count(*) from data;
-- 不全为空的行数
select count(id) from data;
-- 通过主键值查询行数
select count(*) from data where price<200;
select sum(price) from data where type='A';
select max(id) from data;
select min(price) from data;
select max(price) max_price,min(price) min_price from data;
select avg(price) from data where type='c';
null值的处理
分组查询
select sum(price) from data group by type;
select type,count(id) from data group by type;
条件筛选
select type,count(id) count from data group by type having count=4 order by type;
分页查询
分页显示
select * from data limit 5;
-- 从第四条开始依次向后显示五条
select * from data limit 3,5;
insert into select语句
create table data2(
name varchar(10),
price double);
insert into data2 select name,price from data;
select * from data2;
create table data3(
type varchar(10),
num int
);
insert into data3 select type,count(*) from data group by type order by count(*);
select * from data3;
来源:https://blog.csdn.net/qq_63701832/article/details/127933134
0
投稿
猜你喜欢
- 下载python3.6.5安装包1. 上传安装包。打开终端,利用命令cd 进入文件所在文件夹里python@ubun
- 1.在vscode中搜索Autoprofixer2.在安装完成之后要配置在需要添加前缀的css文件上,右键点击命令面板,输入Autopref
- 在Dreamweaver 4.0中,我们就已接触了模板与库的概念,知道它们是批量生成风格类似的网页的好工具。如今在Dreamweaver M
- 如何验证IP地址?sub chkIP(boardid) dim rsIP dim ipAr
- 前沿在CV领域,我们需要熟练掌握最基本的知识就是各种卷积神经网络CNN的模型架构,不管我们在图像分类或者分割,目标检测,NLP等,我们都会用
- 作为设计师,我们都知道,一个极简的设计可以实现漂亮的效果。然而,很多设计师在实现上有些麻烦:要么是没有时间让使用如此少的元素制作的页面看起来
- 利用空闲几天把《JavaScript权威指南》安静的读了一篇。真是一本好书呀!呵呵,这句话见的太多了。好在什么地方呢?听我慢慢道来。从开始接
- 1.外形尺寸尺寸单位:只用默认的像素或者其他字符类的值!,不要用英寸毫米之类的内容。btn = tkinter.Button(root,te
- 目录json支持的格式:代码操作1.json转化为python2. python序列化为json总结json转化为python表示反序列化p
- 很多时候我们的redis的IP地址一般都是默认的127.0.0.1代表只能接受本机的访问,因此我们其他机器上想要访问这个redis的时候,就
- 长期以来一直以为iframe跟div一样都是块级元素,直到今天在一个群中看到一位朋友问到iframe怎么居中的时候,测试了下发现原来我一直对
- 稀疏矩阵格式 coo_matrixcoo_matrix是最简单的稀疏矩阵存储方式,采用三元组(row, col, data)(或称为ijv
- /* --SQLServer中将字符串首字母设置大写: --作者:jinjazz /csdn --SQLServer2005启用OLEAut
- 今天我们来学习,如何使用有趣的自定义标记来布局页面。有的朋友可能有这样的疑问,自己随便定义的标记浏览器怎么能正确的认识呢?这里我们就要用到文
- 在使用Pytorch进行神经网络训练时,有时会遇到训练学习率不下降的问题。出现这种问题的可能原因有很多,包括学习率过小,数据没有进行Norm
- 算法是计算机科学中一个重要的研究方向,是解决复杂问题的关键。在计算机世界中,算法无处不在。数据库是存储数据和执行大批量计算的场所,在数据库中
- 有时需要在网上租用空间或数据库,Mysql成本低一些,所以想将sql server转成mysql……注意:在安装Mysql时要选择文字集为u
- 如下所示:arrs=[2,15,48,4,5,6,7,6,4,1,2,3,6,6,7,4,6,8]f=open('test.txt&
- 正题: 1.1 javascript的灵活性 面向对象对象的Javascript编程模式:1、可以保存状态 2、具有对象内部才能调用的方法
- 参考OpenCV摄像头使用代码import cv2cap = cv2.VideoCapture(4) # 使用第5个摄像头(我的电脑插了5个