将INSERT INTO与VALUE和SELECT + INNER JOIN组合在一起

时间:2015-09-03 08:03:04

标签: mysql sql inner-join sql-insert insert-into

所以基本上我需要结合这个:

INSERT INTO order(B_DATUM, orderNR, L_DATUM)
VALUES ('1999-04-23', '0205', DATE_ADD('1999-04-23', INTERVAL 7 DAY))

用这些:

INSERT INTO bestel( BEDRAG ) 
SELECT 0.92*SUM( bestreg.aantal * bestreg.best_pr ) 
FROM bestreg
INNER JOIN bestel ON bestreg.bestelnr = bestel.bestelnr
WHERE bestel.bestelnr = '0205'

&安培;

INSERT INTO order( lev_code ) 
SELECT offertes.lev_code FROM order
INNER JOIN (orderreg INNER JOIN offertes 
ON orderreg.art_lev = offertes.art_lev)  
ON orderreg.bestelnr = order.ordernr
WHERE order.ordernr = '0205'

我在以前的问题中已经读到你必须将VALUES放在SELECT中但是一旦我进入INNER JOIN它就会搞乱。

提前谢谢!

3 个答案:

答案 0 :(得分:0)

ANSI SQL解决方案,不确定MySQL是否支持它:

INSERT INTO order(B_DATUM, orderNR, L_DATUM)
VALUES ('1999-04-23', '0205', DATE_ADD('1999-04-23', INTERVAL 7 DAY))
union all
VALUES ('1999-04-23', '0205', DATE_ADD('1999-04-23', INTERVAL 7 DAY))
union all
SELECT offertes.lev_code FROM order
INNER JOIN (orderreg INNER JOIN offertes 
ON orderreg.art_lev = offertes.art_lev)  
ON orderreg.bestelnr = order.ordernr
WHERE order.ordernr = '0205'

BTW,ORDER是ANSI SQL中的保留字,您可能需要将其删除,例如"order"或者可能使用反向标记。

答案 1 :(得分:0)

你可以试试这个: -

 INSERT INTO order(B_DATUM, orderNR, L_DATUM, lev_code)
 SELECT ('1999-04-23', '0205', DATE_ADD('1999-04-23', INTERVAL 7 DAY), SELECT offertes.lev_code FROM order
                                                                       INNER JOIN orderreg ON orderreg.bestelnr = order.ordernr 
                                                                       INNER JOIN offertes ON orderreg.art_lev = offertes.art_lev  
                                                                       WHERE order.ordernr = '0205')

答案 2 :(得分:0)

所以我一直在尝试并最终让它工作(MYSQL):

        INSERT INTO order(B_DATUM, orderNR, L_DATUM , bedrag, lev_code)
        SELECT '1999-04-23', '0205', 
        DATE_ADD('1999-04-23', INTERVAL 7 DAY),
        0.92*SUM( bestreg.aantal * bestreg.best_pr ), offertes.lev_code 
        FROM order INNER JOIN (bestreg INNER JOIN offertes 
        ON bestreg.art_lev = offertes.art_lev)  ON bestreg.ordernr = order.ordernr
        WHERE order.ordernr = '0205'

以前对类似问题的回答是正确的,说明你在SELECT中使用VALUES并且只是放弃了' VALUES'声明。

感谢您的评论&回复!