Sagar Karira Sagar Karira - 5 months ago 10
SQL Question

Insert data from first table into second with an extra column not present in the second one

So currently I am using this query:

INSERT INTO tb_first_wallet_transactions (vendor_id, amount, transaction_type, status, checksumhash)
SELECT
vendor_id,
amount,
transaction_type,
status,
checksumhash
FROM tb_second_wallet_transactions
WHERE
id = 1


There is also a column name
type
in
tb_first_wallet_transaction
that can be 0 or 1, that I also need to add, but the problem is that there is no such column in the second table.

One solution is that I run a query which changes does type == 1 after a certain time stamp (i.e of the insertion time of above query)

But I want to do all the insertion in one single query. Is that possible?

Answer

Just add the type in the column list and add a default value in your SELECT:

INSERT INTO tb_first_wallet_transactions (vendor_id, amount, transaction_type, status, checksumhash, type)
    SELECT
        vendor_id,
        amount,
        transaction_type,
        status,
        checksumhash,
        0 -- Choose your default value
    FROM tb_second_wallet_transactions
    WHERE
        id = 1