连接存储过程中的3个表时的重复输出

时间:2014-10-27 08:29:21

标签: mysql stored-procedures

大家好日子!我遇到了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

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

提前谢谢!

1 个答案:

答案 0 :(得分:0)

感谢所有回复的人。无论如何,我终于解决了我的问题。我只是分析为什么我会得到多个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

有!我的问题现在已经解决了。再次感谢你。