gpt4 book ai didi

mysql - 在存储过程中连接 3 个表时出现重复输出

转载 作者:行者123 更新时间:2023-11-29 23:30:55 25 4
gpt4 key购买 nike

大家好!我的 MySql 存储过程出现重复输出的问题,当我在网上查看时,我的语法在某种程度上是正确的。有人可以帮我吗?这是我的代码:

 DELIMITER $$

USE `xxxxxx`$$

DROP PROCEDURE IF EXISTS `VIEW_GCELC_DELEGATE_SP`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `VIEW_GCELC_DELEGATE_SP`(
)
BEGIN
SELECT lc_data.indicode, lc_indi.lastname, lc_indi.firstname, lc_org.orgname
FROM tb_gcelc_data AS lc_data
INNER JOIN tb_gcelc_orgreg AS lc_org ON lc_data.orgcode = lc_org.orgcode
INNER JOIN tb_gcelc_indireg AS lc_indi ON lc_data.orgcode = lc_indi.orgcode
WHERE lc_data.year = YEAR(CURDATE())
ORDER BY lc_org.orgname;
END$$

DELIMITER ;

这是输出:

indicode lastname  firstname orgname

379257 Testing Testing Company Number One
957914 Testing Testing Company Number One
729065 Testing Testing Company Number One
389915 Testing Testing Company Number One
652854 Testing Testing Company Number One
696817 Testing Testing Company Number One
469712 Testing Testing Company Number One
450179 Testing Testing Company Number One
966966 Testing Testing Company Number One
379257 Test Mode Testing Again Company Number One
957914 Test Mode Testing Again Company Number One
729065 Test Mode Testing Again Company Number One
389915 Test Mode Testing Again Company Number One
652854 Test Mode Testing Again Company Number One
696817 Test Mode Testing Again Company Number One
469712 Test Mode Testing Again Company Number One
450179 Test Mode Testing Again Company Number One
966966 Test Mode Testing Again Company Number One
379257 More Test More Company Number One
957914 More Test More Company Number One
729065 More Test More Company Number One
389915 More Test More Company Number One
652854 More Test More Company Number One
696817 More Test More Company Number One
469712 More Test More Company Number One
450179 More Test More Company Number One
966966 More Test More Company Number One

由于循环输出,这很奇怪。

提前谢谢您!

最佳答案

感谢所有回复的人。无论如何,我终于解决了我的问题。我只是分析为什么我会得到多个 Orgname,这是因为:

INNER JOIN tb_gcelc_indireg AS lc_indi ON lc_data.orgcode = lc_indi.orgcode

我把它改成

LEFT JOIN tb_gcelc_indireg AS lc_indi ON lc_data.indicode = lc_indi.indicode

那里!我的问题现在已经解决了。再次感谢您。

关于mysql - 在存储过程中连接 3 个表时出现重复输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26583556/

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