I want to copy columns from table1 into table2 with some additonal columns in table2.
Although I know the syntax:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
But how can I add an additional column's data into table2?
I've tried this approach but it gives syntax error:
// first storing the desired data from table1 into a temporary table
CREATE TEMPORARY TABLE temp_table
select column1, column2, column3 from table1
where condition;
// then placing the selected columns into table2
INSERT INTO table2 (col1, col2, col3,col4) values (
SELECT column1 FROM temp_table,
SELECT column2 FROM temp_table,
SELECT column3 FROM temp_table,
'Additional Value'
);
You can add a litteral string (or any other expression) to the column list. Consider:
INSERT INTO table2 (col1, col2, col3,col4)
SELECT
column1,
column2,
column3,
'Additional Value'
FROM table1
Please, follow below query and you can use alias as extra column it's
static or expression field:
(SQL-Fiddle)
INSERT INTO table2 (col1, col2, col3, col4)
SELECT column1, column2, column3, 'Additional Value' as column4
FROM table1
WHERE condition;
Related
I need to insert data in table 2 but it has a column 3 which is a constant. How to built query for this comdition
?
INSERT INTO table2 (column1, column2, column3)
SELECT column1, column2
FROM table1
?
You can just add your constant value after column2 from the select
INSERT INTO table2 (column1, column2, column3)
SELECT column1, column2, 'your constant value'
FROM table1
I have to insert few items which are dynamic from different tables
insert into (field1, field2, field3) values(<select query fetching one record>,<static value>, <select query fetching multiple records, need to insert one by one>)
how to insert with foreach in db here? Please help!
Sample Data:
insert into (field1, field2, field3) values(select myname from table1',0, select payment[1] from table2);
insert into (field1, field2, field3) values(select myname from table1',0, select payment[2] from table2);
If what you want to insert into your table is the combined result of other queries you can just use INSERT INTO with SELECT and UNION.
Something like this
INSERT INTO your_table (column1, column2, column3)
SELECT column1, column2, column3
FROM table1
UNION ALL
SELECT column1, column2, column3
FROM table2
I want to insert value from TABLE2 (2 field) into another TABLE1 (containing 5 field).
When I execute query:
insert into TABLE1 select (field1, field2) from TABLE2
My rest three fields became null.
I want to insert my new value like current date and new id in TABLE1.
How to do it?
Thanks in advance
TRY THIS
insert into TABLE1 (field1,field2 'value','myval','currentdate') select field1,field2,'your value','other value',NOW() from TABLE2
You can specify new columns next to field1 and field2.
If you are using MS Sql Server, for example:
Insert into table1
Select row_number() over(order by field1) as newID, field1, field2, Getdate(), value3
From table2
I have two tables - Table_1 and Table_2.
They have identical columns - Col1, Col2, Col3, Col4(integer).
Col4 value for all Table_1 rows is 1.
Col4 value for all Table_2 rows is 2.
I insert Table_2 rows into Table_1.
Table_1 and Table_2 have some duplicate values based on Col1 and Col2. I need to retain the Table_2 values and delete the Table_1 duplicate values.
For example:
Col1,Col2,Col3,Col4
1) a ,b ,c ,1 (From Table_1)
2) a ,b ,d ,2 (From Table_2)
I tried the following -
ALTER TABLE Table_1 ADD UNIQUE (Col1, Col2);
It removed the Table_2 values instead of Table_1 values.
How do I solve this problem?
Put a unique key on col1, col2 and use on duplicate key update col3=VALUES(col3), col4=VALUES(col4) when you insert.
Another alternative. Create the unique key on col1, col2 and use the REPLACE INTO syntax.
REPLACE INTO Table_1 (col1, col2, col3, col4)
SELECT col1, col2, col3, col4 FROM Table_2
If a duplicate record exists, it will delete the record in Table_1 and insert the record from Table_2. If the record doesn't exist, it simply inserts it.
I want to transfer 50 rows from table1 to table 2, how should I do this?
Suggested code:
INSERT INTO table2 SELECT * FROM table1
WHERE uid IN ('23','34','345','567','3242','34322','2') DELETE;
Write 2 statements. If you usee InnoDB, put them inside a transaction:
START TRANSACTION ;
INSERT INTO table2
SELECT *
FROM table1
WHERE uid IN ('23','34','345','567','3242','34322','2') ;
DELETE FROM table1
WHERE uid IN ('23','34','345','567','3242','34322','2') ;
COMMIT ;
INSERT INTO table1 (col1, col2, col3) SELECT col1, col2, col3 FROM table2 WHERE col4='some value'
http://www.databasejournal.com/features/mssql/article.php/3507171/Transferring-Data-from-One-Table-to-Another.htm
INSERT INTO TABLE2 SELECT * FROM TABLE1 WHERE COL1 = 'A'