我想问一下,如何自动增加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_product应为216。
感谢您的帮助!
答案 0 :(得分:0)
我认为您正在寻找id列的AUTO_INCREMENT规范。 您应该查看此链接,以便正确理解如何管理您的表:http://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html
答案 1 :(得分:0)
步骤1:设置字段id_product_attribute自动增量
ALTER TABLE ps_product_attribute MODIFY COLUMN id_product_attribute INT auto_increment
步骤2:从插入脚本中删除列id_product_attribute并创建procudure(将数字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步:运行程序
答案 2 :(得分:0)
创建一个过程,以便从预先指定的数字生成id_product
值到所需的最大数量,例如216
,并在循环中使用这些值运行插入。
编写以下示例,假设id_product_attribute
列中的auto_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_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 );