0

我在将我的 informix db 转换为 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):不是唯一的表/别名:'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;

如果你对此有一个好的想法会很棒:)

4

1 回答 1

0

如果你打算使用 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 ;
于 2017-08-02T11:05:05.200 回答