mysql中字段名与保留字冲突

  • 在设计数据库的时候不小心将数据库的字段设置成了其内置的保留字,例如下面的这段:
1
2
3
4
5
6
CREATE TABLE IF NOT EXISTS `test_billing` (
`vendor` varchar(255),
`cn` varchar(255),
`current_date` varchar(255),
`cost` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 这样你在执行类似下面的查询时:
1
select cn, cost from test_billing where current_date like"2018-10%";

返回值中什么都没有,还带了一个warning:

1
Empty set, 1 warning (0.00 sec)

原因就是字段current_date与MySQL内置的保留字冲突了,那么这时候你还急需查看这些数据,比较快的方法就是:在冲突字段上加反引号 current_date,即

1
select cn, cost from test_billing where `current_date` like"2018-10%";

就可以解决了。


-------------The End-------------

Title:mysql中字段名与保留字冲突

Author:cloud sjhan

Publish Time:2018年12月04日 - 15:12

Last Update:2018年12月04日 - 15:12

Original Link:https://cloudsjhan.github.io/2018/12/04/mysql中字段名与保留字冲突/

License: By-NC-ND 4.0 international

cloud sjhan wechat
subscribe to my blog by scanning my public wechat account
keep going, keep coding
0%
;