数据迁移脚本INSERT INTO

时间:2013-01-31 11:44:26

标签: mysql sql insert-into

我创建了以下SQL代码来迁移我们的数据库。 UP查询工作正常,但我无法弄清楚如何修复DOWN查询。我只想插入payment_gateway表中的所有数据,但也必须匹配正确的reseller_shop.id。

// UP
INSERT INTO payment_gateway (`type`, account, keyIn, keyOut, resellerShopId)
SELECT 'paymentGatewayOgone', rs.ogoneAccount, rs.ogoneSignatureIn, rs.ogoneSignatureOut, rs.id
FROM reseller_shop AS rs
WHERE rs.ogoneAccount <> '';

// DOWN
INSERT INTO reseller_shop (ogoneAccount, ogoneSignatureIn, ogoneSignatureOut)
SELECT pg.account, pg.keyIn, pg.keyOut
FROM payment_gateway AS pg
WHERE pg.type = 'paymentGatewayOgone'
AND reseller_shop.id = pg.resellerShopId

3 个答案:

答案 0 :(得分:2)

似乎我必须使用UPDATE而不是INSERT INTO。

UPDATE reseller_shop AS rs, payment_gateway AS pg
SET rs.ogoneAccount = pg.account, rs.ogoneSignatureIn = pg.keyIn, rs.ogoneSignatureOut = pg.keyOut
WHERE pg.type = 'paymentGatewayOgone'
AND rs.id = pg.resellerShopId

答案 1 :(得分:1)

INSERT INTO reseller_shop (ogoneAccount, ogoneSignatureIn, ogoneSignatureOut)
SELECT pg.account, pg.keyIn, pg.keyOut FROM payment_gateway AS pg 
WHERE pg.type = 'paymentGatewayOgone' AND  pg.resellerShopId in 
(select reseller_shop.id from reseller_shop_id)

希望这有帮助

答案 2 :(得分:0)

有些事情是这样的:

INSERT INTO reseller_shop (ogoneAccount, ogoneSignatureIn, ogoneSignatureOut)
SELECT pg.account, pg.keyIn, pg.keyOut
FROM payment_gateway AS pg 
    INNER JOIN reseller_shop AS rs ON  rs.id = pg.resellerShopId
WHERE pg.type = 'paymentGatewayOgone'

或:

INSERT INTO reseller_shop (ogoneAccount, ogoneSignatureIn, ogoneSignatureOut)
SELECT pg.account, pg.keyIn, pg.keyOut
FROM payment_gateway AS pg 
WHERE pg.type = 'paymentGatewayOgone' 
    AND EXISTS (SELECT * 
                FROM reseller_shop AS rs 
                WHERE rs.id = pg.resellerShopId)