简体   繁体   中英

SQL Query INSERT INTO SELECT

I have a problem about transferring values from one table to another.

Below are my tables and columns:

TABLE: a_logs

a_log_id - status - status_date - description

1 - null - null - hello world

TABLE: a_logs_history

a_hist_id - a_log_id - status - status_date - description

1 - 1 - 5 - 2013-10-19 - hello world
2 - 1 - 7 - 2013-10-25 - hello world

I want to insert recent status w/c is 7 and recent status_date w/c is 2013-10-25 values from a_logs_history table to a_logs table.

I tried using INSERT INTO SELECT but I got an error.

Help me please? Thanks.

Try this

INSERT INTO a_logs VALUES (1,(SELECT status,status_date FROM a_logs_history WHERE  recent status = 7 ,  status_date = '2013-10-25'),'XXX')

In the above example i represented status date as string, it may change as you declared..

Hope it helps!!

Try this:

We can copy only the columns we want to into another, existing table:

INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

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