Insert into Select From Delete - mysql

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'

Related

How to copy one table into another with some additional Data

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;

How to insert into TABLE1 (field1,field2 'value','myval','currentdate') select(field1,field2) 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

ON DUPLICATE KEY UPDATE - help needed

My problem is i have an insert that updates the query on duplicate key and it is like the one below:
INSERT INTO TABLE
(COL1, COL2, COL3 , ETC...)
SELECT
COLA1, COLA2, COUNT(1) , ETC...
FROM TABLE2
WHERE 'CONDITION'
GROUP BY COL1, COL2, COL3
ON DUPLICATE KEY UPDATE COL1=VALUES(COLA1), COL3=COUNT(1)
THIS QUERY RETURNS AN ERROR: General error: 1111 Invalid use of group function SQL
COL1, COD2, COL3 ARE COMPLEX KEY.
Try this:
INSERT INTO TABLE(COL1, COL2, COL3, ETC...)
SELECT COLA1, COLA2, COUNT(1), ETC...
FROM TABLE2
WHERE 'CONDITION'
GROUP BY COL1, COL2, COL3
ON DUPLICATE KEY UPDATE COL1 = VALUES(COL1), COL3 = VALUES(COL3);
That is, refer to the names in the values1 statement, not the expressions in the select statement.

Remove duplicates condition to column value

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.

How to insert a table conditionally

I have a table tbl1(col1, col2, col3, ..., coln), col1 and col2 together are the primary keys. I am trying to insert records from a temporary table #t to tbl1 using following query
insert into tbl1(col1, col2, col3, ..., colm)
select #t.col1, #t.col2, #t.col3, ..., #t.colm
from #t where col2 <> #t.col2
However, I got following error , The multi-part identifier "tblDailyBalanceHistory.BalanceDate" could not be bound. How do I fix my query?
I think your problem is with your WHERE criteria. You can't say col2 <> #t.col2 because col2 hasn't been defined.
Try something like this:
insert into tbl1(col1, col2, col3, ..., colm)
select #t.col1, #t.col2, #t.col3, ..., #t.colm
from #t
left join tbl1 on #t.col2 = tbl1.col2
where tbl1.col2 is null
Good luck.