我想根据最后插入的记录捕获列的主键值。下面是表格结构:
SELECT CAT.Category_type_Name,
CAT.Description,
CAT.Image_url,
CAT.Price,
CAT.Weight,
Sum(TD.Quantity)'Quantity' from tbl_Transaction_Detail as TD
inner join tbl_Category_type as CAT
on TD .Category_Type_ID= CAT.Category_type_ID
WHERE TD.Category_Type_ID = 31
ORDER BY CAT.Category_type_Name,
CAT.Description,
CAT.Image_url,
CAT.Price,
CAT.Weight
存储过程:
create table test
(
id varchar(100) not null primary key,
rmain varchar(100),
rpart bigint
);
请指教。我查看了Delimiter $$
DROP PROCEDURE IF EXISTS insTest$$
Create Procedure insTest()
Begin
Set @rmain := (select trim(concat('DNB', DATE_FORMAT(CURRENT_DATE(), '%y'), DATE_FORMAT(CURRENT_DATE(), '%m'))));
IF ((trim(DATE_FORMAT(CURRENT_DATE(),'%m')) = 01) OR (trim(DATE_FORMAT(CURRENT_DATE(),'%m')) = 1)) THEN
Set @rpart = 1;
END IF;
IF ((trim(DATE_FORMAT(CURRENT_DATE(),'%m')) != 01) OR (trim(DATE_FORMAT(CURRENT_DATE(),'%m')) != 1)) THEN
Set @rpart := (select coalesce(max(rpart),0) from test) + 1;
END IF;
insert into Test (ID, rmain, rpart) values (concat(@rmain,@rpart),@rmain,@rpart);
End$$
DELIMITER ;
,但它适用于仅设置last_insert_ID()
的主键列。提前谢谢......
答案 0 :(得分:2)
为什么呢?如果您获得select max(id)
或获得select id from tbl1 order by id desc limit 1
会怎样?
见Transaction In MySQL
。另外,将事务隔离级别设置为READ COMMITTED
declare last_id INT;
START TRANSACTION;
INSERT INTO tbl1(id,col1,col2) values(1001,'test','test');
SELECT last_id = id FROM tbl1 ORDER BY id DESC LIMIT 1
COMMIT;
答案 1 :(得分:0)
<uses-permission>
这样你就可以访问最后插入的id
了