STR_TO_DATE 作为列,但未找到列?

mysqlmysqli database

您可以使用 having 子句。让我们首先创建一个表 −

mysql> create table DemoTable
   -> (
   -> AdmissionDate varchar(100)
   -> );
Query OK, 0 rows affected (0.55 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable values('10/12/2017');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('01/11/2018');
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values('31/01/2019');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('09/06/2019');
Query OK, 1 row affected (0.16 sec)

mysql> insert into DemoTable values('19/04/2019');
Query OK, 1 row affected (0.23 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

输出

+---------------+
| AdmissionDate |
+---------------+
| 10/12/2017    |
| 01/11/2018    |
| 31/01/2019    |
| 09/06/2019    |
| 19/04/2019    |
+---------------+
5 rows in set (0.00 sec)

以下是对 STR_TO_DATE 作为列的查询 −

mysql> select STR_TO_DATE(AdmissionDate, '%d/%m/%Y') as newDate from DemoTable
   -> having newDate >= '2019-01-01';

输出

+------------+
| newDate    |
+------------+
| 2019-01-31 |
| 2019-06-09 |
| 2019-04-19 |
+------------+
3 rows in set (0.00 sec)

相关文章