MySQL变更表在字段已经存在之前或之后添加字段

我有这个,但它不起作用:

$query = "ALTER TABLE `".$table_prefix."posts_to_bookmark` ADD `ping_status` INT( 1 ) NOT NULL BEFORE `onlywire_status`"; 

我很感激!

 $query = "ALTER TABLE `".$table_prefix."posts_to_bookmark` ADD COLUMN `ping_status` INT(1) NOT NULL AFTER `<TABLE COLUMN BEFORE THIS COLUMN>`"; 

我相信你需要有“添加列”,并使用“后”,而不是“之前”。

如果你想把列放在表的开头,使用“FIRST”语句:

 $query = "ALTER TABLE `".$table_prefix."posts_to_bookmark` ADD COLUMN `ping_status` INT(1) NOT NULL FIRST"; 

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

为了避免乱七八糟的乱七八糟的代码,使用heredocs,所以复制/粘贴,读取和修改更容易,希望它能派上用场:

“之后”的例子:

 $query = <<<EOT ALTER TABLE `{$table_prefix}posts_to_bookmark` ADD COLUMN `ping_status` INT(1) NOT NULL AFTER `<TABLE COLUMN BEFORE THIS COLUMN>`; EOT; 

“第一”例子:

 $query = <<<EOT ALTER TABLE `{$table_prefix}posts_to_bookmark` ADD COLUMN `ping_status` INT(1) NOT NULL FIRST EOT;