如何自动增加mysql表的行值

如何自动增加mysql表的行值,mysql,Mysql,我想问一下,如何自动增加MySQL表的行值 我有这张桌子: INSERT INTO `ps_product_attribute` (`id_product_attribute`, `id_product`, `reference`, `supplier_reference`, `location`, `ean13`, `upc`, `wholesale_price`, `price`, `ecotax`, `quantity`, `weight`, `unit_price_impact`, `d

我想问一下,如何自动增加MySQL表的行值

我有这张桌子:

INSERT INTO `ps_product_attribute` (`id_product_attribute`, `id_product`, `reference`, `supplier_reference`, `location`, `ean13`, `upc`, `wholesale_price`, `price`, `ecotax`, `quantity`, `weight`, `unit_price_impact`, `default_on`, `minimal_quantity`, `available_date`) VALUES
(140, 2, '', '', '', '', '', 0.000000, 7.317073, 0.000000, 100, 0.000000, 0.00, 1, 1, '0000-00-00'),
(141, 2, '', '', '', '', '', 0.000000, 14.634146, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(142, 2, '', '', '', '', '', 0.000000, 24.390244, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(143, 3, '', '', '', '', '', 0.000000, 7.317073, 0.000000, 100, 0.000000, 0.00, 1, 1, '0000-00-00'),
(144, 3, '', '', '', '', '', 0.000000, 14.634146, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(145, 3, '', '', '', '', '', 0.000000, 24.390244, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00');
这描述了两种具有三个属性的产品。如何为213种其他产品(如此处所示)实现此功能:

INSERT INTO `ps_product_attribute` (`id_product_attribute`, `id_product`, `reference`, `supplier_reference`, `location`, `ean13`, `upc`, `wholesale_price`, `price`, `ecotax`, `quantity`, `weight`, `unit_price_impact`, `default_on`, `minimal_quantity`, `available_date`) VALUES
(140, 2, '', '', '', '', '', 0.000000, 7.317073, 0.000000, 100, 0.000000, 0.00, 1, 1, '0000-00-00'),
(141, 2, '', '', '', '', '', 0.000000, 14.634146, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(142, 2, '', '', '', '', '', 0.000000, 24.390244, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(143, 3, '', '', '', '', '', 0.000000, 7.317073, 0.000000, 100, 0.000000, 0.00, 1, 1, '0000-00-00'),
(144, 3, '', '', '', '', '', 0.000000, 14.634146, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(145, 3, '', '', '', '', '', 0.000000, 24.390244, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00');
(146, 4, '', '', '', '', '', 0.000000, 7.317073, 0.000000, 100, 0.000000, 0.00, 1, 1, '0000-00-00'),
(147, 4, '', '', '', '', '', 0.000000, 14.634146, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(148, 4, '', '', '', '', '', 0.000000, 24.390244, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(149, 5, '', '', '', '', '', 0.000000, 7.317073, 0.000000, 100, 0.000000, 0.00, 1, 1, '0000-00-00'),
(150, 5, '', '', '', '', '', 0.000000, 14.634146, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(151, 5, '', '', '', '', '', 0.000000, 24.390244, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00');
.
.
.
最后一个id_产品应为216


谢谢你的帮助

我认为您正在寻找id列的自动增量规范。
为了正确理解如何管理表格,您应该查看此链接:

步骤1:设置字段id\u product\u属性自动递增

ALTER TABLE ps_product_attribute MODIFY COLUMN id_product_attribute  INT auto_increment
步骤2:从insert脚本中删除列id\u product\u属性并创建过程(将编号1000替换为所需的产品编号)。例如:

DELIMITER $$

DROP PROCEDURE IF EXISTS abc $$
CREATE PROCEDURE abc()
BEGIN
   DECLARE a INT Default 1 ;
      simple_loop: LOOP
         SET a=a+1;
         INSERT INTO `ps_product_attribute` (`id_product`, `reference`, `supplier_reference`, `location`, `ean13`, `upc`, `wholesale_price`, `price`, `ecotax`, `quantity`, `weight`, `unit_price_impact`, `default_on`, `minimal_quantity`, `available_date`) VALUES
(a, '', '', '', '', '', 0.000000, 7.317073, 0.000000, 100, 0.000000, 0.00, 1, 1, '0000-00-00'),
(a, '', '', '', '', '', 0.000000, 14.634146, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00'),
(a, '', '', '', '', '', 0.000000, 24.390244, 0.000000, 100, 0.000000, 0.00, 0, 1, '0000-00-00');
         IF a=1000 THEN
            LEAVE simple_loop;
         END IF;
   END LOOP simple_loop;

END $$

DELIMITER ;

步骤3:运行过程

创建一个过程,从预先指定的数字到所需的最大数字生成
id\u产品
值,例如
216
,并在循环中使用这些值运行insert

编写以下示例时,假设
id\u product\u属性在
auto\u increment
列中

示例

delimiter //
drop procedure add_dummy_records //
create procedure add_dummy_records( inout id_product_start int, in max_id int )
begin
  declare start_id int;
  -- declare query text;

   set start_id = id_product_start;
  set @query := 'INSERT INTO `ps_product_attribute` 
                 (`id_product`, `reference`, `supplier_reference`, 
                  `location`, `ean13`, `upc`, 
                  `wholesale_price`, `price`, `ecotax`, 
                  `quantity`, `weight`, `unit_price_impact`, 
                  `default_on`, `minimal_quantity`, `available_date`
                 ) VALUES\n';

  adding_rows: loop
    if ( start_id > id_product_start ) then
      set @query := concat( @query, ',\n' );
    end if;

    set @query := concat( @query, '( ', start_id, ', \'\', \'\', \'\', \'\', \'\', 
                          \'0.000000\', 7.317073, \'0.000000\', 100, \'0.000000\', \'0.00\', 
                          1, 1, \'0000-00-00\' ),\n' );
    set @query := concat( @query, '( ', start_id, ', \'\', \'\', \'\', \'\', \'\', 
                          \'0.000000\', 14.634146, \'0.000000\', 100, \'0.000000\', \'0.00\', 
                          0, 1, \'0000-00-00\' ),\n' );
    set @query := concat( @query, '( ', start_id, ', \'\', \'\', \'\', \'\', \'\', 
                          \'0.000000\', 24.390244, \'0.000000\', 100, \'0.000000\', \'0.00\', 
                          0, 1, \'0000-00-00\' ),\n' );

    if ( start_id = max_id ) then
      set id_product_start := start_id;
      leave adding_rows;
    else
      set start_id := start_id + 1;
    end if;
  end loop adding_rows;

  prepare stmt from @query;
  execute stmt;
  drop prepare stmt;
end;
//
delimiter ;
现在,您可以使用
id\u product
的起始值和最大值来设置所有SP

set @id_start := 5;
set @max_id := 216;
call add_dummy_records( @id_start, @max_id );

-- check if if id_start > 5 and is equal to max_id
select ( @id_start > 5 ), ( @id_start = @max_id );

真的不清楚你在问什么…嗨。是的,因为我不懂sql;/我想增加id_product(最多216个)和id_product_属性的价格(足够)。比如第二个例子我不知道你想要什么?但是如果您想让列id\u product\u属性自动递增,请在mysql中设置。但是如何通过mysql查询使其自动?可能是这样。我有第一个例子中的表格。只有2个产品(ID 2和3)我想通过sql查询用其他214个ID手动填写此表。。。第一列台阶为140141142。。。第二列的步长是2,2,2,3,3,3,价格列的步长是7.317073,14.634146,24.390244,7.317073,14.634146,24.390244,7.317073,14.634146,24.390244,我在第一个例子中有类似表格的。只有2个产品(ID 2和3)我想通过sql查询用其他214个ID手动填写此表。。。第一列台阶为140141142。。。第二列的步长是2,2,2,3,3,3,价格列的步长是7.317073,14.634146,24.390244,7.317073,14.634146,24.390244,7.317073,14.634146,24.39024thanks,但我不能删除id_product_属性列:(它是系统的一部分。该字段将自动递增,您不会从系统中删除。啊,好:)我有这个问题#1064-您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解在“@a INT Default 1”附近使用的正确语法;简单循环:循环集@a=@a+1;第4行的INS感谢大家的努力和时间!我找到了解决这个问题的简单方法,将数据库导出到csv,在excel中编辑,保存到csv,然后导入到数据库。工作很有魅力@siwydym:我想它一定是在
drop
语句中。将其更改为
删除过程(如果存在),添加\u伪\u记录//
。在那之后,我注意到这个#1064-您的SQL语法有一个错误;检查与您的MySQL服务器版本相对应的手册,以了解使用near'的正确语法;设置查询:=concat(@query,start\u id,,,,,,,,,,,,,,,,,,在第行13@siwydym:concat未正确关闭。已更正。感谢大家的努力和时间!我找到了一个简单的方法来解决此问题,方法是将数据库导出到csv,在excel中编辑,保存到csv,然后导入到数据库。效果很好!@Siwydy:恭喜。