Skip column when inserting into MySQL

2019-05-26 14:50发布

How can I skip a column when inserting a new row into a table without knowing neither the names nor the amount of columns in my table?

Using INSERT (col1, col2) VALUES (1, 2) is not an option, because I can not know the amount of columns during runtime. It's all calculated from user input.

Thus, I need to skip the first column (id, PRIMARY KEY auto_increment) when inserting.

标签: mysql insert
2条回答
混吃等死
2楼-- · 2019-05-26 15:31

You can insert without giving column names, but you had to give some values for all columns.

INSERT INTO comments 
VALUES (null, 2, 3, 4,null,6) 

CREATE TABLE IF NOT EXISTS `comments` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `imageid` int(10) unsigned NOT NULL DEFAULT '0',
  `uid` bigint(20) unsigned NOT NULL DEFAULT '0',
  `content` text CHARACTER SET utf8,
  `adate` datetime DEFAULT NULL,
  `ip` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
   KEY `ids` (`imageid`,`adate`) USING BTREE
) ENGINE=InnoDB  DEFAULT CHARSET=latin1  ;
查看更多
孤傲高冷的网名
3楼-- · 2019-05-26 15:34

Try inserting 0 as the first value. If the column is auto-incremented it should work.

From MySQL reference: "No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign NULL or 0 to the column to generate sequence numbers. " http://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html

查看更多
登录 后发表回答