MySQL---year()、month()

本文介绍如何使用SQL查询Orders表中2020年1月的订单号(order_num)和订单日期(order_date),并按日期升序展示结果,展示了year()和month()函数的应用。

日期作"特定日期"含义时,要用到year,month等日期函数,不然其数据类型只是单纯的数字,不会有其背后的时间逻辑.

year函数以int数据类型的格式返回特定日期的年度信息。month函数以int数据类型的格式返回特定日期的月份信息。

year()表示指定的日期,month()表示指定的月份。

Orders订单表

order_numorder_date
a00012020-01-01 00:00:00
a00022020-01-02 00:00:00
a00032020-01-01 12:00:00
a00042020-02-01 00:00:00
a00052020-03-01 00:00:00

【问题】编写 SQL 语句,返回 2020 年 1 月的所有订单的订单号(order_num)和订单日期(order_date),并按订单日期升序排序

【示例结果】

返回订单号order_num,和order_date订单时间

order_numorder_date
a00012020-01-01 00:00:00
a00032020-01-01 12:00:00
a00022020-01-02 00:00:00

【示例解析】

a0001、a0002、a0003 时间属于2020年1月

select order_num,order_date from Orders
where year(order_date) = 2020 and month(order_date) = 1
order by order_date

在between中使用日期时,SQL会默认将date格式转换为datetime格式,具体操作就是为date格式后面添加" 00:00:00",若是order_date between "2020-01-01" and "2020-01-31",其实是between "2020-01-01 00:00:00" and "2020-01-31 00:00:00,2020-01-31这一天实际上是没有被囊括进去的。

mysql-connector-j 是 MySQL 官方提供的 Java 连接器,用于在 Java 程序中连接 MySQL 数据库。它的文件结构如下: ``` mysql-connector-java-x.x.xx.jar ├── META-INF │ ├── MANIFEST.MF │ └── maven │ └── mysql │ └── mysql-connector-java │ ├── pom.properties │ └── pom.xml ├── com │ └── mysql │ ├── jdbc │ │ ├── Blob.class │ │ ├── CallableStatement.class │ │ ├── Clob.class │ │ ├── Connection.class │ │ ├── DatabaseMetaData.class │ │ ├── Date.class │ │ ├── Driver.class │ │ ├── DriverManager.class │ │ ├── ParameterMetaData.class │ │ ├── PreparedStatement.class │ │ ├── ResultSet.class │ │ ├── ResultSetMetaData.class │ │ ├── RowId.class │ │ ├── Savepoint.class │ │ ├── SQLClientInfoException.class │ │ ├── SQLException.class │ │ ├── SQLData.class │ │ ├── SQLInput.class │ │ ├── SQLOutput.class │ │ ├── SQLPermission.class │ │ ├── SQLWarning.class │ │ ├── SQLXML.class │ │ ├── Statement.class │ │ ├── Struct.class │ │ ├── Time.class │ │ ├── Timestamp.class │ │ └── Types.class │ ├── jdbc2 │ │ ├── optional │ │ │ ├── Blob.class │ │ │ ├── Clob.class │ │ │ ├── Connection.class │ │ │ ├── DatabaseMetaData.class │ │ │ ├── Date.class │ │ │ ├── ParameterMetaData.class │ │ │ ├── PreparedStatement.class │ │ │ ├── ResultSet.class │ │ │ ├── ResultSetMetaData.class │ │ │ ├── Statement.class │ │ │ ├── Time.class │ │ │ ├── Timestamp.class │ │ │ └── Types.class │ │ ├── optional │ │ │ ├── Blob.class │ │ │ ├── Clob.class │ │ │ ├── Connection.class │ │ │ ├── DatabaseMetaData.class │ │ │ ├── Date.class │ │ │ ├── ParameterMetaData.class │ │ │ ├── PreparedStatement.class │ │ │ ├── ResultSet.class │ │ │ ├── ResultSetMetaData.class │ │ │ ├── Statement.class │ │ │ ├── Time.class │ │ │ ├── Timestamp.class │ │ │ └── Types.class │ │ └── optional │ │ ├── Blob.class │ │ ├── Clob.class │ │ ├── Connection.class │ │ ├── DatabaseMetaData.class │ │ ├── Date.class │ │ ├── ParameterMetaData.class │ │ ├── PreparedStatement.class │ │ ├── ResultSet.class │ │ ├── ResultSetMetaData.class │ │ ├── Statement.class │ │ ├── Time.class │ │ ├── Timestamp.class │ │ └── Types.class │ └── statements │ ├── CallableStatement.class │ ├── PreparedStatement.class │ ├── Statement.class │ └── StatementImpl.class └── java └── time ├── Clock.class ├── Duration.class ├── Instant.class ├── LocalDate.class ├── LocalDateTime.class ├── LocalTime.class ├── Month.class ├── MonthDay.class ├── OffsetDateTime.class ├── OffsetTime.class ├── Period.class ├── Year.class ├── YearMonth.class ├── ZonedDateTime.class └── ZoneId.class ``` 其中,最重要的文件是 `mysql-connector-java-x.x.xx.jar`,这是一个 Java 归档文件,包含了连接 MySQL 所需的所有类和资源。在该文件中,`META-INF` 目录下是元数据信息,`com.mysql.jdbc` 是连接 MySQL 所需的核心类,`java.time` 是 Java 8 中的时间 API,用于与 MySQL 中的日期和时间数据交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值