我们可以在MySQL中通过DATEPART()选择行吗?可能吗?
DATEPART()
MySQL中没有函数,您需要使用MONTH()
函数从日期列中提取月份名称。语法如下:
SELECT *FROM yourTableName WHERE MONTH(yourDateColumnName)=yourValue;
为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table SelectRowFromDatePart-> (
-> Id int NOT NULL AUTO_INCREMENT,
-> LoginDate date,
-> PRIMARY KEY(Id)
-> );
现在,您可以使用insert命令在表中插入一些记录。查询如下:
mysql> insert into SelectRowFromDatePart(LoginDate) values(curdate());mysql> insert into SelectRowFromDatePart(LoginDate) values(date_add(curdate(),interval -3 year));
mysql> insert into SelectRowFromDatePart(LoginDate) values('2014-05-21');
mysql> insert into SelectRowFromDatePart(LoginDate) values('2015-08-25');
mysql> insert into SelectRowFromDatePart(LoginDate) values('2018-12-31');
mysql> insert into SelectRowFromDatePart(LoginDate) values('2011-12-21');
mysql> insert into SelectRowFromDatePart(LoginDate) values('2013-10-02');
mysql> insert into SelectRowFromDatePart(LoginDate) values('2006-12-11');
mysql> insert into SelectRowFromDatePart(LoginDate) values('2010-03-09');
mysql> insert into SelectRowFromDatePart(LoginDate) values('2012-12-12');
使用select语句显示表中的所有记录。查询如下:
mysql> select *from SelectRowFromDatePart;
以下是输出:
+----+------------+| Id | LoginDate |
+----+------------+
| 1 | 2019-01-31 |
| 2 | 2016-01-31 |
| 3 | 2014-05-21 |
| 4 | 2015-08-25 |
| 5 | 2018-12-31 |
| 6 | 2011-12-21 |
| 7 | 2013-10-02 |
| 8 | 2006-12-11 |
| 9 | 2010-03-09 |
| 10 | 2012-12-12 |
+----+------------+
10 rows in set (0.00 sec)
这是按日期部分选择查询的查询,即MONTH()
:
mysql> select *from SelectRowFromDatePart where MONTH(LoginDate)=12;
以下是输出:
+----+------------+| Id | LoginDate |
+----+------------+
| 5 | 2018-12-31 |
| 6 | 2011-12-21 |
| 8 | 2006-12-11 |
| 10 | 2012-12-12 |
+----+------------+
4 rows in set (0.00 sec)
以上是 我们可以在MySQL中通过DATEPART()选择行吗?可能吗? 的全部内容, 来源链接: utcz.com/z/335260.html