Duplicating all SQL rows and simultaneously changing the data - mysql

I would like to duplicate every row in my table, but in the duplicate row the value of one column will need to overwrite an existing value in another column (an intermediary step in normalising my tables).
So what I need is something like
INSERT INTO `performers` SELECT * FROM `performers`
SET `performers`.`piano` = `performers`.`singing`
As I'm no good at SQL the above, as expected, didn't work. Any ideas?

If only the duplicate row needs to have this values changed, then you can modify your INSERT:
INSERT INTO `performers`(col1, col2, col3, piano, singing)
SELECT col1, col2, col3, singing, singing
FROM `performers`

From your question I think you want to duplicate the row and then copy content of singing column into piano column?
INSERT INTO performers (row1, row2, row3, piano)
SELECT row1, row2, row3, singing
FROM performers

Related

Copy row from one MySQL DB to another

How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.

Select Insert Query by mentioning all columns across databases

I am working on redesigning of a legacy db and I have set new names to columns of old db. So, for instance, if olddb.oldtable under dbold has column descr, I have set it as description in new newdb.netable for column.
How can I mention individual columns in my query?
I am using MYSQL
Update: Both Databases are on different IP Addresses and I am using Navicat to transfer data.
You can try like this:
INSERT INTO newtable (col1, col2, ..., )
SELECT col1, col2, ..., FROM oldtable
By trying the above query you can insert the specific column. So for example if your newtable has a column as description and old table as descr then you can mention it like:
INSERT INTO newtable (col1, col2, `description`, ..., )
SELECT col1, col2, `descr` ,..., FROM oldtable
Also if the table column list is large and you want to copy all the columns and its data then you can simply use the wildcard charater * as:
INSERT INTO newtable
SELECT * FROM oldtable;
You can insert all columns at once without the need to mention the names using this:
INSERT INTO newtable (SELECT * FROM oldtable);
It will make an 1x1 match independently of column names.
If types don't match then will insert default values (not checked for all the type combination).
Note that column number must be the same on both tables otherwise an error like this will occur:
#1136 - Column count doesn't match value count at row 1

How to move row to another row in mysql

I want to move one or two rows(with data) for e.g. from end to first position or from middle to end? With phpmyadmin with GUI there isn't option to moving rows.
Another question:
How to move one table to another table by copying data?
Rows position in a resultset are determined by ORDER BY clause or by "chance" if it's not specified, so moving from a position to another in absolute has no meaning.
You can use INSERT SELECT statement to copy data from table1 to table2 if they have the same structure.
INSERT INTO table2
SELECT *
FROM table1
You may copy data with help of a query:
INSERT INTO table2 (col1, col2, col3) SELECT col1, col2, col3 FROM table1

Inserting data from one table to another without duplication in access-VBA

I want to insert data from a table WorkTableA to another table TableA, without duplicating the data (i.e. do not insert into WorkTableA if the customer name already exists).
Is there a way of doing it through VBA code.
The field name and their properties in both tables are identical.
What you need is an INSERT INTO statement
INSERT INTO WorkTableA
( CustomerName, Col2, Col3...)
SELECT CustomerName, Col2, Col3
FROM TableA LEFT JOIN WaorkTableA ON TableA.CustomerName = WORKTABLEA.CustomerName
WHERE WorkTableA.CustomerName IS NULL
Something like this might work.
The SELECT part of the statement will select only the ones that DO NOT EXIST in WorkTableA

MySQL, inserting row if not exists [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to use if not exists while inserting a row mysql
I have problem:
I have table named "table" with 4 columns: id(int, PK, AI, Unique), col1(varchar), col2(varchar), col3(datetime)
Many users can connect to mysql server and insert rows into "table". The problem is that col2 and col3 can't exists in other row in "table".
I would write something like that:
IF NOT EXISTS (select * from table where col2='2' and col3='2012-12-12 12:12:12') INSERT INTO table(col1, col2, col3) values(1,2,'2012-12-12 12:12:12')
I assume you know what I'm trying to do.
I'm trying to do this in one statement (to avoid situation in which 2 users inserts the same row at the same time) or in transaction. If I should do it in transaction, then what type of isolation should it be? Serializable isolation causes lock tables so it can slow down inserting process much more.
Help me, please.
If you don't want duplicates for the pair of col2 and col3 (i.e. col 2 can have duplicates but any pair of col2 and col3 only appears once) you can specify this:
UNIQUE(col2,col3)
Any query which now attempts to insert a col2,col3 pair will fail. You can deal with this either using:
INSERT IGNORE INTO table(col1, col2, col3) values(1,2,'2012-12-12 12:12:12');
or if the insert should change the values you can use:
INSERT INTO table(col1, col2, col3) values(1,2,'2012-12-12 12:12:12')
ON DUPLICATE KEY UPDATE someCol=someVal