begin declare i int default 1; DECLARE theNumOfZero2 INT ; DECLARE temp_ProductNum VARCHAR(10) DEFAULT "";
while i<10000 do SET theNumOfZero2= 5-LENGTH(i); IF theNumOfZero2=1 THEN set temp_ProductNum= CONCAT("0",i); ELSEIF theNumOfZero2=2 THEN set temp_ProductNum= CONCAT("00",i); ELSEIF theNumOfZero2=3 THEN set temp_ProductNum= CONCAT("000",i); ELSEIF theNumOfZero2=4 THEN set temp_ProductNum= CONCAT("0000",i); END IF;
set @OrderID1 = CONCAT('BS20101027',temp_ProductNum);
select @MaxID:=MAX(ID) from `order_list`; INSERT order_product_list(OrderID,ProductID,Quantity,Price) VALUES (@MaxID+1,'S0001201005190001',1,25.19);
INSERT order_product_list(OrderID,ProductID,Quantity,Price) VALUES (@MaxID+1,'S0001201005190002',1,25.19); set i=i+1; end while; end这个存储过程中select @MaxID:=MAX(ID) from `order_list`;为什么查询不出他的最大ID
begin declare i int default 1; DECLARE theNumOfZero2 INT ; DECLARE temp_ProductNum VARCHAR(10) DEFAULT "";
while i<10000 do SET theNumOfZero2= 5-LENGTH(i); IF theNumOfZero2=1 THEN set temp_ProductNum= CONCAT("0",i); ELSEIF theNumOfZero2=2 THEN set temp_ProductNum= CONCAT("00",i); ELSEIF theNumOfZero2=3 THEN set temp_ProductNum= CONCAT("000",i); ELSEIF theNumOfZero2=4 THEN set temp_ProductNum= CONCAT("0000",i); END IF;
set @OrderID1 = CONCAT('BS20101027',temp_ProductNum);
select @MaxID:=MAX(ID) from `order_list`; INSERT order_product_list(OrderID,ProductID,Quantity,Price) VALUES (@MaxID+1,'S0001201005190001',1,25.19);
INSERT order_product_list(OrderID,ProductID,Quantity,Price) VALUES (@MaxID+1,'S0001201005190002',1,25.19); set i=i+1; end while; end这个存储过程中select @MaxID:=MAX(ID) from `order_list`;为什么查询不出他的最大ID
select MAX(ID) into @MaxID from `order_list`
大部分情况下是这样,MYSQL会自动利用索引进行SQL语句的优化,但一些情况下还是需要人为的干预。
我查询出来的就是null,但是我单独查询的时候能查询出来.
select @MaxID:=MAX(ID) from `order_list`; select @MaxID; 有无结果
declare i int default 1;
DECLARE theNumOfZero2 INT ;
DECLARE temp_ProductNum VARCHAR(10) DEFAULT "";
while i<10000 do
SET theNumOfZero2= 5-LENGTH(i);
IF theNumOfZero2=1 THEN
set temp_ProductNum= CONCAT("0",i);
ELSEIF theNumOfZero2=2 THEN
set temp_ProductNum= CONCAT("00",i);
ELSEIF theNumOfZero2=3 THEN
set temp_ProductNum= CONCAT("000",i);
ELSEIF theNumOfZero2=4 THEN
set temp_ProductNum= CONCAT("0000",i);
END IF;
set @OrderID1 = CONCAT('BS20101027',temp_ProductNum);
INSERT order_list (OrderID,UserID,ContactEmail,OrderStatus,ShippingPrice,TotalPrice,PaymentCurrencyID,HasHardware,DeliveryStatus,ExpressTypeID,ShipperID,PrincipalID,CreateDate,Addr_Type,Insurance,Addr_Country,OrderType,SendMailStatus,PaymentTypeID)
VALUES
(@OrderID1,'BSU2010112900002','[email protected]',1,0.00,242.18,2,1,1,1,'bluesoleil','bluesoleil','2010-11-26 15:48:52','BlueSoleil Web',0,'CN',0,0,1);
select @MaxID:=MAX(ID) from `order_list`;
INSERT order_product_list(OrderID,ProductID,Quantity,Price)
VALUES
(@MaxID+1,'S0001201005190001',1,25.19);
INSERT order_product_list(OrderID,ProductID,Quantity,Price)
VALUES
(@MaxID+1,'S0001201005190002',1,25.19);
set i=i+1;
end while;
end这个存储过程中select @MaxID:=MAX(ID) from `order_list`;为什么查询不出他的最大ID
declare i int default 1;
DECLARE theNumOfZero2 INT ;
DECLARE temp_ProductNum VARCHAR(10) DEFAULT "";
while i<10000 do
SET theNumOfZero2= 5-LENGTH(i);
IF theNumOfZero2=1 THEN
set temp_ProductNum= CONCAT("0",i);
ELSEIF theNumOfZero2=2 THEN
set temp_ProductNum= CONCAT("00",i);
ELSEIF theNumOfZero2=3 THEN
set temp_ProductNum= CONCAT("000",i);
ELSEIF theNumOfZero2=4 THEN
set temp_ProductNum= CONCAT("0000",i);
END IF;
set @OrderID1 = CONCAT('BS20101027',temp_ProductNum);
INSERT order_list (OrderID,UserID,ContactEmail,OrderStatus,ShippingPrice,TotalPrice,PaymentCurrencyID,HasHardware,DeliveryStatus,ExpressTypeID,ShipperID,PrincipalID,CreateDate,Addr_Type,Insurance,Addr_Country,OrderType,SendMailStatus,PaymentTypeID)
VALUES
(@OrderID1,'BSU2010112900002','[email protected]',1,0.00,242.18,2,1,1,1,'bluesoleil','bluesoleil','2010-11-26 15:48:52','BlueSoleil Web',0,'CN',0,0,1);
select @MaxID:=MAX(ID) from `order_list`;
INSERT order_product_list(OrderID,ProductID,Quantity,Price)
VALUES
(@MaxID+1,'S0001201005190001',1,25.19);
INSERT order_product_list(OrderID,ProductID,Quantity,Price)
VALUES
(@MaxID+1,'S0001201005190002',1,25.19);
set i=i+1;
end while;
end这个存储过程中select @MaxID:=MAX(ID) from `order_list`;为什么查询不出他的最大ID
select @MaxID;
有无结果
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
select @MaxID:=MAX(ID) from `order_list`;
select @MaxID;
放入SP中运行,检查结果
我这数据库,关联的表太多了.比较复杂.如果这样写的话我估计大家都会看上半小时去了.我还是抓重点吧.
我用的是MySql5
select @MaxID:=MAX(ID) from `order_list`;
insert into lsb values(@MaxID);
select @MaxID:=MAX(ID) from `order_list`;
是取到MAX(ID)的值的
二是表设计方面