rank是MySQL版本8.0.2中定义的MySQL保留字。因此,不能将rank用作列名。你需要在排名周围使用反勾号。
让我们首先检查我们正在处理的MySQL版本。在这里,我正在使用MySQL版本8.0.12-mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.12 |
+-----------+
1 row in set (0.00 sec)
使用“ rank”作为列名的问题如下-mysql> create table DemoTable1596
-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> StudentName varchar(20),
-> rank int
-> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'rank int
)' at line 5
在上面,由于我们使用保留字作为列名,因此可见错误。
让我们首先创建一个表并在“ rank”周围使用反引号以避免错误-mysql> create table DemoTable1596
-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> StudentName varchar(20),
-> `rank` int
-> );
使用插入命令在表中插入一些记录-mysql> insert into DemoTable1596(StudentName,`rank`) values('Bob',4567);
mysql> insert into DemoTable1596(StudentName,`rank`) values('David',1);
使用select语句显示表中的所有记录-mysql> select * from DemoTable1596;
这将产生以下输出-+----+-------------+------+
| Id | StudentName | rank |
+----+-------------+------+
| 1 | Bob | 4567 |
| 2 | David | 1 |
+----+-------------+------+
2 rows in set (0.00 sec)