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


72

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

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

我很感激!

Answers:


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

相信您需要拥有ADD COLUMN和使用AFTER,而不是BEFORE

如果要将列放在表的开头,请使用以下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


33
是的,之后或优先,而不是之前。+1
dwich 2010年

太棒了。谢谢!
atwellpub,2010年

1
为了便于使用,BEFORE可以理解“为什么要将这个字段放在那里”。当我将行“字段”的第一位作为目标时,我只需添加“ fieldname
#AKA
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.