gpt4 book ai didi

mysql - 重复条目 Mysql 主数据库

转载 作者:行者123 更新时间:2023-11-30 01:31:59 25 4
gpt4 key购买 nike

此代码中出现一些错误,尝试过调试但找不到答案。

有人可以纠正我吗?

BEGIN

DECLARE FINALID VARCHAR(8);
DECLARE MAXIDCOMPANY VARCHAR(8);

SELECT MAX(SUBSTRING(company.idCompany,3,8))+1 INTO MAXIDCOMPANY
FROM company
WHERE SUBSTRING(company.idCompany,1,2) = SUBSTRING(nmCompany,1,2);


IF MAXIDCOMPANY > 0 AND MAXIDCOMPANY < 10 THEN
SET FINALID = CONCAT(SUBSTRING(nmCompany,1,2),'00', CONVERT(MAXIDCOMPANY USING latin1));
ELSEIF MAXIDCOMPANY > 9 AND MAXIDCOMPANY < 100 THEN
SET FINALID = CONCAT(SUBSTRING(nmCompany,1,2),'0', CONVERT(MAXIDCOMPANY USING latin1));
ELSEIF MAXIDCOMPANY > 99 THEN
SET FINALID = CONCAT(SUBSTRING(nmCompany,1,2), CONVERT(MAXIDCOMPANY USING latin1));
END IF;

IF MAXIDCOMPANY IS NULL THEN
SET FINALID = CONCAT(SUBSTRING(nmCompany,1,2),'001');
END IF;

INSERT INTO company
(company.idCompany, company.nmCompany, company.country, company.address, company.region, company.joinDate, company.quota, company.createdUser, company.createdDate, company.updatedUser, company.updatedDate)
VALUES
(FINALID, nmCompany, country, address, region, joinDate, quota, createdUser, NOW(), updatedUser, updatedDate);



END

这就是代码,程序在接近 1000 后停留在计数器上。

示例:

当我添加数据(例如 IN1000)作为 idCompany 时,它总是停留在该数字上,但是当我查询最大数据时,它只显示 IN999,为什么不显示 IN1000

非常感谢

最佳答案

因为max函数与SUB-STRING结合会返回变量字符内的最大值,解决办法是用count函数代替,逻辑计数会返回最大值。

SELECT COUNT(company.idCompany) + 1 INTO MAXIDCOMPANY from company where SUBSTRING(company.idCompany,1,2) = SUBSTRING(company.idCompany,1,2);

根据我的商业散文,谢谢大家的回答..

关于mysql - 重复条目 Mysql 主数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17340814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com