jjzjj

SQL——数据查询DQL

小平凡的记录 2023-04-29 原文

基本语句、时间查询(当天、本周,本月,上一个月,近半年的数据)。

目录

1 查询语句基本结构

2 where 子句

3 条件关系运算符

4 条件逻辑运算符

5 like 子句

6 计算列

7 as 字段取别名

8 distinct 清除重复行

9 排序 order by

10 聚合函数 

10.1 count() 统计函数

10.2 max() 最大值函数

10.3 min() 最小值函数

10.4 avg() 平均值函数

11 日期函数

12 字符串函数

13 分组查询 group by

14 分页查询 limit

15 时间查询语句


1 查询语句基本结构

# 查询指定列
select colnumName1[,colnumName2,colnumName3...] from <tableName> [where
conditions];
# 查询到的记录的所有列,则可以使⽤ * 替代字段名列表
select * from <tableName>;

——查询全部列,也可指定某列

select s_num,s_name,s_sex,s_age,s_dept from student;

select * from student;

where 子句

在删除、修改及查询的语句后都可以添加where⼦句(条件),⽤于筛选满⾜特定的添

加的数据进⾏删除、修改和查询操作。
delete from tableName where conditions;
update tabeName set ... where conditions;
select .... from tableName where conditions;

条件关系运算符

## = 等于
select * from student where s_num='230101';

## != <> 不等于
select * from student where s_num !='230101';
select * from student where s_num <> '230101';

## > ⼤于
select * from student where s_age>23;

## < ⼩于
select * from student where s_age<23;

## >= ⼤于等于
select * from student where s_age>=23;

## <= ⼩于等于
select * from student where s_age<=23;

## between and 区间查询 between v1 and v2 [v1,v2]
select * from student where s_age between 23 and 24;

条件逻辑运算符

# and 并且 筛选多个条件同时满⾜的记录
select * from student where s_age=23 and s_sex='女';

# or 或者 筛选多个条件中⾄少满⾜⼀个条件的记录
select * from student where s_age=23 or s_sex='女';

# not 取反
select * from student where s_age not between 22 and 24;

like 子句

# 查询学⽣姓名包含'张一'的学⽣信息
select * from student where s_name like '张一';

# 查询学⽣姓名第⼀个字为'张'的学⽣信息
select * from stus where stu_name like '张%';

# 查询学⽣姓名最后⼀个字⺟为'一'的学⽣信息
select * from student where s_name like '%一';

# 查询学⽣姓名中第⼆个字为小的学⽣信息
select * from student where s_name like '_小%';

% 表示任意多个字符 【 %一% 包含字一】

_ 表示任意⼀个字符  【 _小% 第⼆个字为小】

计算列

对某列进行计算并显示。 

select s_name,2023-s_age from student;

7 as 字段取别名

 在上面计算的基础下,重新定义一个列名。

select s_name,2023-s_age as s_birthday from student;

distinct 清除重复行

从查询的结果中将重复的记录清除。
select s_age from student;

9 排序 order by

将查询到的满⾜条件的记录按照指定的列的值升序(asc)、降序(desc)排列。
select * from tableName where conditions order by columnName asc|desc;

# 单字段排序
select * from student where s_age>22 order by s_sex desc;

# 多字段排序
select * from student where s_age>22 order by s_sex asc,s_age desc;

10 聚合函数 

进⾏计算的函数count 、max 、min 、sum 、avg。

10.1 count() 统计函数

统计满足条件的指定字段值的个数(记录数) 。

# 统计学生表总数
select count(stu_num) from stus;

# 统计学⽣表中性别为女的学⽣总数
select count(s_num) from student where s_sex='女';

10.2 max() 最大值函数

查询满足条件的记录中指定列的最大值。

select max(s_age) from student;

select max(s_age) from student where s_sex='男';

10.3 min() 最小值函数

查询满足条件的记录中指定列的最小值。

select max(s_age) from student;

select max(s_age) from student where s_sex='男';

10.4 avg() 平均值函数

查询满足条件的记录中计算指定列的平均值。

select avg(s_age) from student;

select avg(s_age) from student where s_sex='男';

11 日期函数

  • 向日期类型的列添加数据时,可以通过字符串类型赋值(字符串的格式必须为 yyyy-MM-dd hh:mm:ss)
  • 获取当前系统时间添加到日期类型的列,可以使⽤ now() 或者 sysdate()
# 创建新列——时间
alter table student add date datetime;

12 字符串函数

 对字符串进⾏处理。

  • concat :concat(colnum1,colunm2,...) 拼接多列
  • upper:upper(column) 将字段的值转换成⼤写
  • lower:lower(column) 将指定列的值转换成⼩写
  • substring:substring(column,start,len) 从指定列中截取部分显示 start从1开始
select concat(s_name,'-',s_sex) from student;
select upper(s_name) from student;
select lower(s_name) from student;
select s_name,substring(s_dept,1,3) from student;

13 分组查询 group by

select 分组字段/聚合函数
from 表名
[where 条件]
group by 分组列名 [having 条件]
[order by 排序字段]
select 后通常显示分组字段和聚合函数(对分组后的数据进行统计、求和、平均值等) 语句执⾏属性:
  • 先根据where条件从数据库查询记录
  • group by对查询记录进⾏分组
  • 执⾏having对分组后的数据进⾏筛选
# 先对查询的学⽣信息按性别进⾏分组(分成了男、⼥两组),然后再分别统计每组学⽣的个数
select s_sex,count(s_num) from student group by s_sex;

# 先对查询的学⽣信息按性别进⾏分组(分成了男、⼥两组),然后再计算每组的平均年龄
select s_sex,avg(s_age) from student group by s_sex;

# 先对学⽣按年龄进⾏分组,然后统计各组的学⽣数量,还可以对最终的结果排序
select s_age,count(s_num) from student group by s_age order by s_age;

# 查询所有学⽣,按年龄进⾏分组,然后分别统计每组的⼈数,再筛选当前组⼈数>1的组,再按年龄升序显示出来
select s_age,count(s_num) from student
group by s_age
having count(s_num)>1
order by s_age;

# 查询性别为'女'的学⽣,按年龄进⾏分组,然后分别统计每组的⼈数,再筛选当前组⼈数>1的组,再按年龄升序显示出来
select s_age,count(s_num) from student
where s_sex='女'
group by s_age
having count(s_num)>1
order by s_age;

14 分页查询 limit

select ...
from ... 
where ...
limit param1,param2
param1 int :表示获取查询语句的结果中的第⼀条数据的索引(索引从0开始)
param2 int:表示获取的查询记录的条数(如果剩下的数据条数<param2,则返回剩下的所有记录)

15 时间查询语句

——今天

select * from 表名 where to_days(时间字段列名) = to_days(now());

——昨天

select * from <tablename> where to_days(now()) - to_days(时间字段名)=1;

——近7天

select * from 表名 where date_sub(curdate(),interval 7 day) < date(时间字段名);

——本周

select * from 表名 where yearweek(date_format(时间字段名,'%Y-%m-%d')) = yearweek(now());

——上周

select * from 表名 where yearweek(date_format(时间字段名,'%Y-%m-%d')) = yearweek(now())-1;

——近7天

select * from 表名 where date_sub(curdate(),interval 7 day) < date(时间字段名);

——近30天

select * from 表名 where date_sub(curdate(),interval 30 day) < date(时间字段名);

——本月

select * from 表名 where date_format(时间字段名, '%Y%m') = date_dormat(curdate(),'%Y%m');

——上个月

select * from 表名 where period_diff(date_format(now(),'%Y%M'), date_format(时间字段名,'%Y%m'))=1;

 ——本季度

select * from 表名 where quarter(时间字段名) =quarter(now());

——上季度

select * from 表名 where quarter(时间字段名)=quarter(date_sub(now(),interval 1 quarter));

——本年

select * from 表名 where year(时间字段名, '%Y%m') = year(now());

——上年

select * from 表名 where year(时间字段名) = year(date_sub(now(),interval 1 year));

有关SQL——数据查询DQL的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  4. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  5. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  6. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  7. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  8. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  9. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

  10. STM32读取串口传感器数据(颗粒物传感器,主动上传) - 2

    文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,

随机推荐