0

With with reference to this questions aswer where we transfer contents from one table to another, The doubt that I am having is that ,How to go further if we have to add another column's value which is not there in old table but ,should be there in new table..

ie: from INSERT INTO newTable (col1, col2, col3)SELECT column1, column2, column3 FROM oldTable

to

INSERT INTO newTable (col1, col2, col3,date) SELECT column1, column2, column3 FROM oldTable.

Please note that date column is not there in old table but should be in new table.Yes ofcourse we can include insert query with help of id ,but it becomes 2 separate queries.But can this be done using a single query..

Community
  • 1
  • 1
codelover
  • 317
  • 1
  • 11

1 Answers1

0

Your query would be

    INSERT INTO newTable (col1, col2, col3) SELECT column1, column2, column3 FROM oldTable.

Because you are determing column names where you are inserting the data. Therefor no need to even mention the date column.

EDIT:

Let's say you need to insert data only in to the "date" column, then you would do something like this

Your query would be (when date column data doesnt come from another table )

    INSERT INTO newTable (date) VALUES dateVALUE

BR's

user2831723
  • 832
  • 12
  • 25
  • ,The thing is I have a scenario wherein There is a need for extra column only in new table.How to insert separate value only for that column???. – codelover Feb 14 '14 at 19:16
  • I edited my answer, I hope I didint misunderstand your question :) – user2831723 Feb 14 '14 at 19:28
  • ,Ya you are absolutely correct by writing insert query separately.But it becomes 2 queries..But I need it in a single query – codelover Feb 14 '14 at 19:37
  • what are you trying to achieve with that ? If you would be aquiring data from multiple tables then it would be fairly easy to use LEFT JOIN and ON/AND but you are creating a new blank column and insterting new data to it. I can't see any advantage in one single query. – user2831723 Feb 14 '14 at 20:14