2

I am trying to select already inserted data and want to insert same data into same table. Just one field is change .So i have tried something like this but it is not working.

INSERT INTO TABLENAME (field2, field3,2) 
SELECT field2,field3 FROM TABLENAME

Please help.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
  • possible duplicate of [mysql -> insert into tbl (select from another table) and some default values](http://stackoverflow.com/questions/5907206/mysql-insert-into-tbl-select-from-another-table-and-some-default-values) – Nican Oct 21 '14 at 06:12

2 Answers2

1

First you have to specify fieldName in Insert Query and In select query you have to specify new value which you want to insert like :

INSERT INTO TABLENAME (field2, field3, field4) 
SELECT field2,field3,2 FROM TABLENAME
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
  • Thanks for help. It works and next i want to do is "I have two table TABLE1 and TABLE2. Now I want to update value in relational table(TABLE2) with matching a ID of Master table(TABLE1) with another table ID." . How can i do this? – Bhumi Shah Oct 21 '14 at 06:19
  • I can accept answer after 5 mins as per stackoverflow – Bhumi Shah Oct 21 '14 at 06:20
1

You have mixed up column names with their values. The insert into clause use column names which determine where the data should be inserted to. In the select list you can have column names, constant or calculations:

INSERT INTO tablename (field2, field3, field_with_constant_value) 
SELECT      field2, field3, 2 
FROM        tablename
Mureinik
  • 297,002
  • 52
  • 306
  • 350