可能重复:
MySQL:如果列不存在,如何添加?
我使用的一个工具正在运行这个查询,但它失败了。我正在尝试帮助调试,但无法找出问题所在:
IF EXISTS( SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'TOWNY_RESIDENTS'
AND table_schema = 'minecraft'
AND column_name != 'town-ranks')
THEN
ALTER TABLE TOWNY_RESIDENTS (ADD
`town-ranks` mediumtext,
`nation-ranks` mediumtext
);
内部选择查询工作正常。似乎 if exists 语法是错误的,但我不知道怎么做。像这样的网站上的例子显示了类似的想法......
错误是:
#1064 - 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 'IF EXISTS( SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'TOW' at line 1