添加表时脚本出现mysql错误

Gil*_*kaz 4 mysql database apache scripting

Mysql错误(请不要笑)

我制作了一个脚本,为我的网站生成统计表.此脚本生成超过2000个表,这些表用于具有不同粒度的不同统计数据,如:

  • 桌子多年
  • 白天的桌子
  • 用户的表格
  • user_agent表

等...(不要问我为什么这样,它是由前面所谓的建筑师制作的 - 他们在不到0.02秒的时间内加载)

所以我更新了脚本以添加新表,但我犯了一个错误.

我在表名中放了一个空格,现在我无法删除它.所以我试图放一个\来逃避但没有成功:

root@summary:reports> drop table xd_2012_02_\ ua;
ERROR: 
Unknown command '\ '.
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 '\ ua' at line 1
Run Code Online (Sandbox Code Playgroud)

Boo*_*eus 8

你必须使用` - 这将把传递的字符串视为实际的表名而不是你要执行的一堆命令.

root@summary:reports> drop table `xd_2012_02_\ ua`;
Run Code Online (Sandbox Code Playgroud)

这应该表明:

Query OK, 0 rows affected (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

我建议使用`不仅是这种情况,而且如果你对列或表名使用类似typedesc(作为描述)的保留MySQL字,也可以防止错误.这可能会在以后节省您的麻烦.虽然使用保留或特殊字符不是一个好习惯,但那只是我.