INSERT Query for complete row expect id - mysql

I tried to insert a complete row to a table. But the problem is, that I use the same IDs. Is there a easy way to insert a complete row expect the one value (ID)?
INSERT INTO new_table SELECT * FROM old_table WHERE q_value = 12345
I would not insert every single value, because there are hunderds of columns.
Thanks for your help in advance,
Yab86

May be Something like this
You can do without getting column2
Insert Into new_table (Column1,Column3,Column4 ...)
Select Column1,Column3,Column4 ... From old_table
WHERE q_value = 12345

If ID is part of a unique key the only (good) way is to write out all the other columns, like this
insert into new_table
values (col1, col2, col3)
select col1, col2, col3
from old_table
where q_value = 12345;
If ID isn't part of the unique key there might be some ways to do it in two queries (easier to write but perhaps not better)
insert into new_table
select *
from old_table
where q_value = 12345;
update new_table
set ID = null
where q_value = 12345;

If the first column in new_table is your auto_increment primary id, you could use this, but then you cannot use the asterisk and have to list all columns except the first one (0 will do the trick for your auto_increment, that means it will insert the incremented value of the autoindex):
INSERT INTO new_table
SELECT 0, column2, column3, ...
FROM old_table WHERE q_value = 12345

Related

How to creat new ID's in new table with MYSQL

I've seen lots of questions with similar headlines or interest but different from what I am looking for.
I have a table with some data in it already, with an id column. I want the id's of the data already in the table, a couple thousand rows, to remain the same. I want to INSERT different data from another table, which also includes id's - just over 92,000 rows in this set -- and I need these id's to change to some other non-existing ID so as not to overwrite or displace the already existing data.
Is this possible? Is there some kind of increment I can do upon INSERT?
I tried an INSERT IGNORE statement but it displaced the data already there.
Any advice on what to try?
If id in your first table is an auto_increment column then you can just omit it
INSERT INTO table1 (column2, column3, column4, ...) -- id is omitted
SELECT column2, column3, column4, ... -- id is also omitted
FROM table2
If it's not the case, and assuming that it's a one-time operation, you can try to assign new ids in a following way
INSERT INTO table1 (id, column2, column3, column4, ...)
SELECT t.max_id + s.id, s.column2, s.column3, s.column4, ...
FROM
(
SELECT #n := #n + 1 id, column1
FROM table2 CROSS JOIN (SELECT #n := 0) i
ORDER BY id
) s JOIN
(
SELECT MAX(id) max_id
FROM table1
) t
Here is SQLFiddle demo
To start with an AUTO_INCREMENT value other than 1, set that value with CREATE TABLE or ALTER TABLE, like this:
mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;

Join 2 tables with same PRIMARY key that is autoincrementing too

I have 2 tables with both of them having the first column as PRIMARY which is also an auto incrementing. First table has 67 entries starting from 1 to 67 and the second table has 48 entries. Both of them have same columns. I want to take the content from Table2 and insert them into Table1 such that the next entry in Table1 starts from 68. Finally I will have 115 entries in Table1 and the PRIMARY column will also show that. I tried this:
INSERT INTO `Table1` SELECT * FROM `Table2`
But it said
#1062 - Duplicate entry '1' for key 'PRIMARY'
What do I do?
Name the columns you want to insert and leave the auto_incrment columns from the insert
INSERT INTO `Table1` (col2, col3, col4)
select col2, col3, col4 from `table2`
You need to specify the columns you wish to enter, without the identity column.
Something like
INSERT INTO `Table1` (column1, column2,...,columnn)
SELECT column1, column2,...,columnn FROM `Table2`
SET IDENTITY_INSERT database_name.schema_name.table ON
--Do Insert Here
SET IDENTITY_INSERT database_name.schema_name.table OFF
Make sure there is no duplicate id thought.
See this page
try out this...
INSERT INTO Table1 (col2, col3,...)
select col2, col3,... from Table2
you can do it with n number of columns...
Here you need to specify column name on that you wish to insert data other than identity column(Auto Increment Column).

Duplicating records in the same MySQL table has duplicate entry for key

Is there a way to use a MySQL INSERT similar to the following:
INSERT INTO doc_details SELECT * FROM doc_details WHERE dd_id = 1
This doesn't work because the primary key is being repeated and it can get very long-winded expanding the columns out.
The purpose of this is to duplicate rows in the same table which will get modified later, retrieving the last_insert_id for the new record. So ideas for other ways to do this would be appreciated too.
Thanks.
Simply name the columns you want to duplicate and omit the primary key:
INSERT INTO doc_details (col1, col2, col3)
SELECT col1, col2, col3
FROM doc_details
WHERE dd_id = 1
I'd suggest you to make ID field with AUTO_INCREMENT option, then use NULL values when inserting -
INSERT INTO doc_details(id, column1, column2)
SELECT NULL, column1, column2 FROM doc_details WHERE dd_id = 1;
In this case old ID will be changed with new ones.
You can depend on temporary table to copy from old record and omitting the key field value.
You have to use at least one named column, i.e. the key field name, to omit its repeating values.
See the following example:
CREATE TEMPORARY TABLE tmp SELECT * from doc_details WHERE dd_id = ?;
ALTER TABLE tmp drop pk_field_name_here; -- drop the key field for not repeating
INSERT INTO doc_details SELECT 0, tmp.* FROM tmp;
DROP TABLE tmp;
You can observe that no other filed names are used but the key field name to omit it's value.
You can also refer to my answer to a similar posting at: Mysql: Copy row but with new id.
Thanks for the answers. Really appreciated. Because most answers specify the column, this led to some extra research that said 'wildcards cannot be used in INSERT statements. Select, Modify and insert into the same table
I managed to solve this in my application with a separate SELECT then the INSERT with the columns expanded with a Perl map function:
SELECT * FROM doc_details WHERE dd_id = 1
Then in Perl, with the row as a hash reference in $data:
$data->{'dd_id'} = 0;$columns = join(',', map {$_ .'='. $dbh->quote( $data->{$_} ) } keys %{$cdh} );
Does the trick nicely - it copies the row regardless of changes to the column structure/order as long as the auto_increment column is maintained.
I know it's not a pure SQL solution - although Ravinder provided one that was.
Thanks to all!

MySQL Single Table Scan Update

Is there a way to accomplish a single table scan in MySQL with an UPDATE? The following is a standard example:
IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
INSERT INTO Table1 VALUES (...)
This is the ideal situation I'd like to happen in MySQL (But this is MsSQL):
UPDATE user SET (name = 'jesse') WHERE userid ='10001'
IF ##ROWCOUNT=0
INSERT INTO user (name) VALUES('jeeeeee')
It's sort of reversed in MySQL. You perform the insert, and if the key already exists, then update the row:
INSERT INTO Table1 (col1,col2,col3) VALUES (val1,val2,val3)
ON DUPLICATE KEY UPDATE col1 = val1, col2 = val2, col3 = val3;
This is predicated on you having a unique key for the table (which you do, right?)

Copying a table into another table but preserving the same auto increment key

I have a table A filled with records. I created a table B with same columns, and I want to copy all contents of A to B. However, table A has an auto incremented key, so if i had first three records (1,'itemA') (2,'itemB') (5,'itemE') (assuming that 3,4,5 where deleted later). Those recors will be inserted into table B as (1,'itemA') (2,'itemB') (3,'itemE').
Is there a way to insert them exactly the same ?
Another thing is, table A is on mySql, and table B is on MS SQL Server
AFAIK mysql allow inserting into auto_increment field, so you can use statement like
insert into table2 (id, name) select id, name from table1
but later, if you need insert into table values with generated auto_inc, you need set auto_increment in table2 with value of auto_inc of table1
alter table table2 AUTO_INCREMENT = (SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = $dbName AND TABLE_NAME = 'table1')
Yes.
create table b like a;
insert into b select * from a;
http://sqlfiddle.com/#!2/a344a/1
The answers above are good, but on MS SQL you can't insert auto increment value unless if you execute turn identiy_insert off:
SET IDENTITY_INSERT stock OFF;
INSERT INTO stock ( stock_id,stock_item) VALUES (5,'itemE');
SET IDENTITY_INSERT stock ON;
This is EXCATLY what I was looking for. Thank you all :)
At table with name test and columns (id autoinc, col1, col2, col3)
At table with name test2 and columns (id autoinc, col4, col5, col6)
INSERT INTO test(col4, col5, col6) SELECT col4, col5, col6 FROM test2;