简体   繁体   中英

Insert data to table at once which retrieve from mySQL select command

I have mentioned my Original transaction table and temporary Transaction Table I want to insert data to temporarytransaction table which I select values of Transaction table

    CREATE TABLE Transaction(
    Tid int auto_increment primary key,
    T_type varchar(20),
    Amount decimal(10,2),
    Tdate DATETIME,
    accountNo int,
    CONSTRAINT FOREIGN KEY(AccountNo) REFERENCES Account(AccountNo)
    ); 



CREATE TABLE tempTransaction(
    Tid int auto_increment primary key,
    T_type varchar(20),
    Amount decimal(10,2),
    Tdate DATETIME,
    accountNo int,
    CONSTRAINT FOREIGN KEY(AccountNo) REFERENCES Account(AccountNo)
    );

I want edit this query to working one That is my Question

INSERT INTO temporarytransaction VALUES (SELECT * FROM Transaction WHERE `Tdate` BETWEEN "2012-03-15" AND "2016-03-31"); 

Don't put the values() in; http://dev.mysql.com/doc/refman/5.6/en/insert-select.html .

INSERT INTO temporarytransaction SELECT * FROM Transaction WHERE `Tdate` BETWEEN "2012-03-15" AND "2016-03-31";

Try this:

INSERT INTO temporarytransaction (Tid,T_type,Amount,Tdate,accountNo)
SELECT Tid,T_type,Amount,Tdate,accountNo 
FROM Transaction 
WHERE `Tdate` BETWEEN "2012-03-15" AND "2016-03-31"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM