php - 如何在 MySQL 中设置年份数据类型

标签 php mysql sql types phpmyadmin

我看不懂这条sql语句有什么问题

INSERT INTO tbl_car_model_year_rate
SET car_model_id =4,
FROM =2008,
TO =2011,
with_driver =1,
per_day =1000,
ten_days =10000,
twenty_days =20000,
thirty_days =30000,
image =  '1303166512test.jpg',
created_at = NOW( ) ,
created_by =1

数据类型

car_model_id    int(11) 
image   text        
from    year(4)
to  year(4)
with_driver tinyint(1)          
per_day int(11) 
ten_days    int(11) 
twenty_days int(11)
thirty_days int(11)
created_at datetime 
created_by int(11)

错误信息

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 'from  =2008, to   =2011, with_driver =1, per_day  =1000, ten_days =10000,     twenty' at line 1

最佳答案

如果您将代码更改为:

INSERT INTO tbl_car_model_year_rate SET car_model_id =4,
`FROM` =2008,
`TO` =2011,
with_driver =1,
per_day =1000,
ten_days =10000,
twenty_days =20000,
thirty_days =30000,
image =  '1303166512test.jpg',
created_at = NOW( ) ,
created_by =1

它会起作用的。 如果您在反引号“`”中添加字段和/或表名称,那么您几乎可以为它们使用任何名称。

失败原因
FROM 是保留字(如 select *FROMtablename)
您通过使用 FROM 作为列名来混淆 MySQL (select * FROM FROM WHERE FROM = AND)

我的推荐
不要对字段名使用保留字。
将列名称从 FROM 更改为 YearFromStartYear
TOYearToEndYear

使用保留字作为列名只会让人感到困惑,在所有内容周围添加反引号只会让内容难以阅读和丑陋,同时仍然令人困惑。

关于php - 如何在 MySQL 中设置年份数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5709812/

相关文章:

php - 使用 PHP FreeTDS 扩展从 MSSQL 服务器获取带有 UTF-8 字符集的数据

MySQL 一天中的每个小时一行

sql - 在单个查询中获取分页行和总数

SQL查询打印从A列开始到B列的值

sql - Postgres 窗口函数和异常分组

php - 为MySQL中的所有记录组选择一组记录中的特定记录

javascript - Tinymce 文本颜色

javascript - php 循环中的 JQuery Post 工作并不完美

PHP 上传脚本未发布

mysql - 按列选择非重复项,字段不对应