sql >> Base de Datos >  >> RDS >> Mysql

Insertar EN MySQL DESDE otra tabla

Dado que está seleccionando de una tabla, querrá usar INSERT INTO SELECT FROM consulta:

INSERT INTO campaign_ledger 
(
    `campaign_id`
    , `description`
    , amount
    , balance
    , timestamp
)
SELECT 
    id as campaign_id
    , 'Ported from campaigns' as description
    , budget_remaining as amount 
    , budget_remaining as balance
    , NOW() as timestamp 
FROM campaigns

Utilice únicamente INSERT INTO VALUES cuando está utilizando valores específicos y no seleccionando de una tabla. Si desea utilizar INSERT INTO VALUES entonces su consulta sería así:

INSERT INTO campaign_ledger 
(
    `campaign_id`
    , `description`
    , amount
    , balance
    , timestamp
)
VALUES
(
    1
    , 'test'
    , 100.00
    , 1000.00
    , NOW()
)