0

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

yab86
  • 395
  • 6
  • 24

3 Answers3

1

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
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
1

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
hellcode
  • 2,678
  • 1
  • 17
  • 21
1

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;
Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78