i got 2 tables that i want to combine its data.
the id is my key field (incremental and distinct).
table 1 and table 2 field description for example:
id - name - value
i want to insert all of table 2 data into table 1, they have different data but in some rows the same id.
so when i try to:
INSERT INTO ladatabase.table2 SELECT * from ladatabase.table1;
i get duplicate entry error.
please help me to suggest how can i solve it and combine the data with different id values for the new data.
Alex.
here are 2 ways:
first you can use if the id AUTO INCREMENT
INSERT INTO ladatabase.table2
SELECT '', name, value from ladatabase.table1;
or you find the first free ID and add it
INSERT INTO ladatabase.table2
SELECT id+555 , name, value from ladatabase.table1;
No insert all fields (exclude 'id'-field from SELECT):
INSERT INTO ladatabase.table2 SELECT name,value from ladatabase.table1;
You need to copy all but the ID column, and it will assign new IDs. You need to list all the other columns explicitly. Leaving out the ID column will cause it to get its default value, which are sequential IDs for an auto_increment field.
INSERT INTO table2 (col2, col3, col4, ...)
SELECT col2, col3, col4, ...
FROM table1
Related
I need to create an identical copy of many records in a table. The table has a PK id, which of course will be different, in the freshly-copied records.
For example, let's say i have a scrum_card table, with the following non-unique columns: name, description, board_id
I have a dynamic array of id's of records, which i wish to duplicate: [34,56,32,3445,...]
How do i tell MYSQL, to fetch the data from all those records, and make a batch-insert of those same records?
In "human" syntax it would look something like this: "Select all columns(besides id) from scrum_card where the id's are [array of id's], then duplicate each found record".
Use INSERT INTO ... SELECT... and in the list of columns do not include the id:
insert into scrum_card(name, description, board_id)
select name, description, board_id
from scrum_card
where id in (34,56,32,3445,...)
You can use INSERT using a SELECT result set as the source, instead of a set of literal row tuples with VALUES(...).
INSERT INTO new_table (id, col1, col2, col3...)
SELECT NULL, col1, col2, col3...
FROM old_table
WHERE id IN (34,56,32,3445,...)
Using NULL in place of the id column in the SELECT will return NULL for each row, which will cause new_table to generate a new id value.
But SQL does not have any way to do a wildcard like "all columns except id," besides you typing the column names in.
I want to insert values from different sources. For example
insert into a (('id','name','add'),'college')
select from b where id = 1,'abc'
Here there is no timestamp field in table b
I would rewrite your query as follows:
insert into a (id, name, add, college)
select id, name, add, 'abc' from b where id = 1
With this query, the 4th column in a will be assigned a constant value of 'abc'.
I have two tables in the same database. With same constraint and same column name. Both tables have primary key with auto-increment and I want insert data directly from one table to the other by using following query.
insert into table_name select * from table_name
all data get inserted into table one but auto-increment is not happening.
in image their is same problem(table in image is created for test)
You can't use * you should use the column name without the id (therwise you insert the selected id and is not performed the autoincrement)
insert into table_name ( col1, col2)
select col1, col2 from table_name;
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.
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