Mysql必须格式化才能输入?

时间:2012-07-10 17:21:29

标签: mysql insert

这真的很奇怪,也许有人知道答案,

我有一张这样的表

mysql> describe sweeps;
+----------------+------------------+------+-----+---------------------+-----------------------------+
| Field          | Type             | Null | Key | Default             | Extra                       |
+----------------+------------------+------+-----+---------------------+-----------------------------+
| entity_id      | int(20) unsigned | NO   | PRI | NULL                | auto_increment              |
| fb_id          | varchar(120)     | YES  |     | NULL                |                             |
| title          | varchar(5)       | YES  |     | NULL                |                             |
| first_name     | varchar(30)      | YES  |     | NULL                |                             |
| last_name      | varchar(30)      | YES  |     | NULL                |                             |
| birthday_month | int(2) unsigned  | YES  |     | NULL                |                             |
| birthday_day   | int(2) unsigned  | YES  |     | NULL                |                             |
| birthday_year  | int(4) unsigned  | YES  |     | NULL                |                             |
| created_at     | datetime         | YES  |     | 0000-00-00 00:00:00 |                             |
| last_submit    | timestamp        | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
+----------------+------------------+------+-----+---------------------+-----------------------------+

如果我把它放在一行中就不会接受INSERT

mysql> `sweeps` (entity_id,fb_id,title,first_name,last_name,birthday_month,birthday_day,birthday_year,created_at) VALUES (1,'fooID1','mr','John','Doe',1,1,1999,NOW());
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 '`sweeps` (entity_id,fb_id,title,first_name,last_name,birthday_month,birthday_day' at line 1

但奇怪的是,如果我像这样将它分开,它会毫无错误地接受它吗?这是为什么?

mysql> INSERT INTO `sweeps` (
    ->   entity_id,
    ->   fb_id,
    ->   title,
    ->   first_name,
    ->   last_name,
    ->   birthday_month,
    ->   birthday_day,
    ->   birthday_year,
    ->   created_at
    -> ) VALUES (
    ->   1,
    ->   'fooID1',
    ->   'mr',
    ->   'John',
    ->   'Doe',
    ->   1,
    ->   1,
    ->   1999,
    ->   NOW()
    -> );
Query OK, 1 row affected (0.41 sec)

mysql> SELECT * FROM sweeps;
+-----------+--------+-------+------------+-----------+----------------+--------------+---------------+---------------------+---------------------+
| entity_id | fb_id  | title | first_name | last_name | birthday_month | birthday_day | birthday_year | created_at          | last_submit         |
+-----------+--------+-------+------------+-----------+----------------+--------------+---------------+---------------------+---------------------+
|         1 | fooID1 | mr    | John       | Doe       |              1 |            1 |          1999 | 2012-07-10 10:16:58 | 2012-07-10 10:16:58 |
+-----------+--------+-------+------------+-----------+----------------+--------------+---------------+---------------------+---------------------+
1 row in set (0.04 sec)

2 个答案:

答案 0 :(得分:5)

它没有插入,因为您错过了命令中的INSERT INTO

mysql> `sweeps` (entity_id,fb_id,title,first_name,last_name,...

应该是

mysql> INSERT INTO `sweeps` (entity_id,fb_id,title,first_name,last_name,...

答案 1 :(得分:0)

你有一个“插入”

你的例子中缺少

mysql> `sweeps` (entity_id,