错误 1066 (42000):不唯一 table/alias:'mp' mysql 中的外部联接

ERROR 1066 (42000): Not unique table/alias: 'mp' OUTER JOIN in mysql

我在将我的 informix 数据库转换为 mysql 时遇到问题。 我完成了大部分事情,但有些功能无法正常工作。

DELIMITER //
CREATE PROCEDURE mw_getsvid(mwid INT) RETURNS INT
BEGIN
DECLARE svId INT;
SELECT sv.ID INTO svId
FROM messwert AS mw, messpunkt AS mp, mpzuordnung AS mpz, summvorschrift    AS sv
    LEFT JOIN messpunkt mp ON mw.messpunktid = mp.id
    LEFT JOIN mpzuordnung mpz ON mp.id = mpz.messpunktid
    LEFT JOIN summvorschrift sv ON mpz.summvorschriftid = sv.id
    LEFT JOIN messwert mw ON mpz.zeitraum_von <= mw.datendatum
    LEFT JOIN messwert mw ON mpz.zeitraum_bis > mw.datendatum;
UNION ALL
SELECT sv.ID INTO svId
FROM messwert AS mw, messpunkt AS mp, mpzuordnung AS mpz, summvorschrift AS sv
    RIGHT JOIN messpunkt mp ON mw.messpunktid = mp.id
    RIGHT JOIN mpzuordnung mpz ON mp.id = mpz.messpunktid
    RIGHT JOIN summvorschrift sv ON mpz.summvorschriftid = sv.id
    RIGHT JOIN messwert mw ON mpz.zeitraum_von <= mw.datendatum
    RIGHT JOIN messwert mw ON mpz.zeitraum_bis > mw.datendatum;
RETURN svId;



END //;

DELIMITER ;

显示:错误 1066 (42000):不唯一 table/alias:'mp'

顺便说一句,这是旧的

CREATE PROCEDURE "informix".mw_getsvid(mwid INT)
RETURNING INT;  

DEFINE svId INT;
SELECT sv.ID INTO svId
    FROM MessWert mw, MessPunkt mp,
        OUTER (MPZuordnung mpz, SummVorschrift sv)
    WHERE mw.id = mwid
        AND mw.messpunktid = mp.id
        AND mp.id = mpz.messpunktid
        AND mpz.summvorschriftid = sv.id
        AND mpz.zeitraum_von <= mw.datendatum
        AND mpz.zeitraum_bis > mw.datendatum;

RETURN svId;

END PROCEDURE;

如果你对此有好的想法,那就太好了:)

如果你打算使用 JOIN,你不需要使用 FROM 子句中的所有表,因为你做的是重复工作。如果要将表放在 FROM 子句和 JOIN 中,则应使用不同的别名。你应该这样做:

DELIMITER //
CREATE FUNCTION mw_getsvid(mwid INT) RETURNS INT
BEGIN
DECLARE svId INT;
SELECT sv.ID INTO svId
FROM messwert AS mw
    LEFT JOIN messpunkt mp ON mw.messpunktid = mp.id
    LEFT JOIN mpzuordnung mpz ON mp.id = mpz.messpunktid
    LEFT JOIN summvorschrift sv ON mpz.summvorschriftid = sv.id
    LEFT JOIN messwert mw ON mpz.zeitraum_von <= mw.datendatum
    LEFT JOIN messwert mw ON mpz.zeitraum_bis > mw.datendatum;
UNION ALL
SELECT sv.ID INTO svId
FROM messwert AS mw
    RIGHT JOIN messpunkt mp ON mw.messpunktid = mp.id
    RIGHT JOIN mpzuordnung mpz ON mp.id = mpz.messpunktid
    RIGHT JOIN summvorschrift sv ON mpz.summvorschriftid = sv.id
    RIGHT JOIN messwert mw ON mpz.zeitraum_von <= mw.datendatum
    RIGHT JOIN messwert mw ON mpz.zeitraum_bis > mw.datendatum;
RETURN svId;

END //;

DELIMITER ;