本文翻译自:Adding multiple columns AFTER a specific column in MySQL

I need to add multiple columns to a table but position the columns after a column called lastname . 我需要向一个表中添加多个列,但将这些列lastname 名为lastname的列之后

I have tried this: 我已经试过了:

ALTER TABLE `users` ADD COLUMN
(`count` smallint(6) NOT NULL,`log` varchar(12) NOT NULL,`status` int(10) unsigned NOT NULL
)
AFTER `lastname`;

I get this error: 我收到此错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AFTER lastname ' at line 7 检查与您的MySQL服务器版本相对应的手册,以在第7行的')AFTER lastname '附近使用正确的语法

How can I use AFTER in a query like this? 如何在这样的查询中使用AFTER?


#1楼

参考:https://stackoom.com/question/1BbIO/在MySQL中的特定列之后添加多个列


#2楼

One possibility would be to not bother about reordering the columns in the table and simply modify it by add the columns. 一种可能性是不用担心重新排序表中的列,而只需通过添加列来对其进行修改。 Then, create a view which has the columns in the order you want -- assuming that the order is truly important. 然后,创建一个具有所需顺序的列的视图-假设顺序确实很重要。 The view can be easily changed to reflect any ordering that you want. 可以轻松更改视图以反映所需的任何顺序。 Since I can't imagine that the order would be important for programmatic applications, the view should suffice for those manual queries where it might be important. 由于我无法想象顺序对于编程应用程序将很重要,因此对于那些可能很重要的手动查询,该视图就足够了。


#3楼

Try this 尝试这个

ALTER TABLE users
ADD COLUMN `count` SMALLINT(6) NOT NULL AFTER `lastname`,
ADD COLUMN `log` VARCHAR(12) NOT NULL AFTER `count`,
ADD COLUMN `status` INT(10) UNSIGNED NOT NULL AFTER `log`;

check the syntax 检查语法


#4楼

If you want to add a single column after a specific field, then the following MySQL query should work: 如果要在特定字段后添加一列,则以下MySQL查询应该起作用:

ALTER TABLE usersADD COLUMN count SMALLINT(6) NOT NULLAFTER lastname

If you want to add multiple columns, then you need to use 'ADD' command each time for a column. 如果要添加多个列,则每次需要为列使用“ ADD”命令。 Here is the MySQL query for this: 这是对此的MySQL查询:

ALTER TABLE usersADD COLUMN count SMALLINT(6) NOT NULL,ADD COLUMN log VARCHAR(12) NOT NULL,ADD COLUMN status INT(10) UNSIGNED NOT NULLAFTER lastname

Point to note 注意事项

In the second method, the last ADD COLUMN column should actually be the first column you want to append to the table. 在第二种方法中,最后一个ADD COLUMN 实际上应该是要添加到表中的第一列。

Eg: if you want to add count , log , status in the exact order after lastname , then the syntax would actually be: 例如:如果您lastname后的确切顺序添加countlogstatus ,则语法实际上是:

ALTER TABLE usersADD COLUMN log VARCHAR(12) NOT NULL AFTER lastname,ADD COLUMN status INT(10) UNSIGNED NOT NULL AFTER lastname,ADD COLUMN count SMALLINT(6) NOT NULL AFTER lastname

#5楼

ALTER TABLE `users` ADD COLUMN
`COLUMN NAME` DATATYPE(SIZE) AFTER `EXISTING COLUMN NAME`;

You can do it with this, working fine for me. 您可以这样做,对我来说很好。


#6楼

This one is correct: 这是正确的:

ALTER TABLE `users`ADD COLUMN `count` SMALLINT(6) NOT NULL AFTER `lastname`,ADD COLUMN `log` VARCHAR(12) NOT NULL AFTER `count`,ADD COLUMN `status` INT(10) UNSIGNED NOT NULL AFTER `log`;

在MySQL中的特定列之后添加多个列相关推荐

  1. mysql自定义序号_MySQL数据库之在mysql中给查询的结果添加序号列

    本文主要向大家介绍了MySQL数据库之在mysql中给查询的结果添加序号列 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 一:第一种: select   (@i:=@i+1)  ...

  2. mysql怎么给数据加序号_MySQL数据库之在mysql中给查询的结果添加序号列

    本文主要向大家介绍了MySQL数据库之在mysql中给查询的结果添加序号列 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 一:第一种: select   (@i:=@i+1)  ...

  3. mysql中如何将存在分隔符号的一列进行拆分,成为多行

    mysql中如何将存在分隔符号的一列进行拆分,成为多行 -- ****************************** 写在前面的说明 ****************************** ...

  4. mysql读取自增值失败_MySQL添加自增列失败

    想往一个mysql表里添加一个自增列做主键,居然失败报告无法读取,这是怎么回事? 1.问题描述 有位朋友在升级discuz论坛数据库时遇到问题了,想给一个表添加自增列做主键,结果发生下面的报错: my ...

  5. mysql中怎么加全文索引_MySQL添加全文索引(一)

    1.ngram and MeCab full-text parser plugins 全文检索在MySQL里面很早就支持了,只不过一直以来只支持英文.缘由是他从来都使用空格来作为分词的分隔符,而对于中 ...

  6. mysql里面取列里面值_在MySQL中获取特定的列值(名称)

    要获取特定的列值,请使用LIKE子句.让我们首先创建一个表-mysql> create table DemoTable1809 ( Name varchar(20) ); 使用插入命令在表中插入 ...

  7. mysql查询序号_在mysql中给查询的结果添加序号列

    一:第一种: select  (@i:=@i+1)   as   i,table_name.*   from   table_name,(select   @i:=0)   as   it 例如: ( ...

  8. mysql按特定的顺序_如何在MySQL中按特定顺序排列数据?

    使用ORDER BYIF()可以按特定顺序排列数据.以下是语法-select *from yourTableName ORDER BY IF(yourColumnName=yourValue1 OR ...

  9. 在mysql中删除数据后,添加数据,id值依然从删除的位置开始增加,问题详解。

    +----+--------------+-----+----------------------+----------+ | id | name | age | email | isActive | ...

最新文章

  1. 腾讯技术工程 | 腾讯数据平台部总监刘煜宏:这5大产品平台,展示了腾讯大数据的核心能力...
  2. Tensorflow2.0与Tensorflow1.x不兼容问题
  3. 子弹短信体验分析:一个单纯想“快”的IM工具 子弹短信体验分析:锤科情怀缩影,打败微信有点远
  4. Android中RelativeLayout各个属性的含义
  5. InetAddressImpl#lookupAllHostAddr慢/挂起
  6. 国际旅游管理专业跨专业考计算机,旅游管理考研我想跨专业考旅游管理专业的研究 – 手机爱问...
  7. 什么是计算机与网络的接口,什么是计算机接入网络的接口设备
  8. 服务器系统开机提示0xc000007b,0xc000007b蓝屏解决方法
  9. easyUI设置表格中的背景色
  10. 2022 年最新博客专家申请流程
  11. 用python 画美国地图
  12. 怎么用计算机算组合数c,排列组合c怎么算 公式是什么
  13. kickstarter众筹
  14. Ubuntu 12.10 禁用来宾账户和远程登录
  15. java--吸血鬼数的判断
  16. TCP的FIN/RST Cookie
  17. php实现视频转gif,一种在线视频转GIF的方法与流程
  18. ORB_SLAM3 Track线程详解
  19. 21. 程序员生存定律-选公司前要干的事:分类
  20. linux设置定时任务(crontab)

热门文章

  1. 一起来开发Android的天气软件(一)——功能规格介绍
  2. 騰訊大廈有39層的問題解決方案。
  3. ANativeWindow是个什么东西
  4. 算法---------两个数的交集
  5. android TextView 文本里面设置超链接
  6. java web每天定时执行任务(四步轻松搞定)
  7. vim的保存文件和退出命令
  8. iOS进阶之架构设计MVVM的理解(3)
  9. mysql 高并发加锁_Mysql高并发加锁事务处理
  10. php链接数据库实行增删查改_利用PHP连接数据库——实现用户数据的增删改查的整体操作实例...