我在数据库表中使用带反引号的“ from”和“ to”作为列标题。现在,如何选择它们?
首先,要使用保留字,应使用反引号将其设置为-
`from` `to`
由于您要选择使用反引号设置的列名(如上所示),因此需要实现以下内容-
select `to`,`from` from yourTableName;
让我们首先创建一个表,其列名称为from和to,并带有反引号-
mysql> create table DemoTable720 ( `from` date, `to` date );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable720 values('2019-01-21','2019-07-23'); mysql> insert into DemoTable720 values('2017-11-01','2018-01-31'); mysql> insert into DemoTable720 values('2015-08-11','2016-09-19');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable720;
这将产生以下输出-
+------------+------------+ | from | to | +------------+------------+ | 2019-01-21 | 2019-07-23 | | 2017-11-01 | 2018-01-31 | | 2015-08-11 | 2016-09-19 | +------------+------------+ 3 rows in set (0.00 sec)
以下是选择“to”和“from”的查询-
mysql> select `to`,`from` from DemoTable720;
这将产生以下输出-
+------------+------------+ | to | from | +------------+------------+ | 2019-07-23 | 2019-01-21 | | 2018-01-31 | 2017-11-01 | | 2016-09-19 | 2015-08-11 | +------------+------------+ 3 rows in set (0.00 sec)