21849

Insert select with a twist question

I Want to copy all the data of a table (with a certain id ofcourse) into the same table, but with a slightly difference.

I have this table (product_quantity):

id----groupId----productId----quantity 1 ----- 2 ------------2--------------5

What i want to do is to copy all the data that have groupId=2, insert it in the table with groupId=3

So I found that I could do a select-insert

insert into product_quantity (groupId, productId, quantity) SELECT groupId, productId, quantity FROM prouct_quantity WHERE groupId=2

But this will only make duplicates.

Is it possible to use this insert-select, but also insert a groupId=3, with just one query?

Thanks in advance!

Answer1:

Try this sql query:

insert into product_quantity (groupId, productId, quantity) select 3, productId, quantity from product_quantity where groupId = 2

Recommend

  • How to make this multidimensional array into three specified parts?
  • Insert nested json array into mysql
  • annotation based DI with Producers
  • awk output into BC HexToDec calculation
  • Hightlight numbers in phone number
  • Unity object not rotating around correct axis
  • Difference between Component-Scan and @Component ?
  • Insert select with a twist question
  • Moving a camera
  • Can null be inserted into Cache?
  • Changing what attribute is displayed in drop down association filter in active admin with Rails 3.1
  • App crashing when buying in-app purchase
  • Unable to render html in jquery autocomplete MVC4
  • Laravel 5.2 'The file “Cover.jpg” was not uploaded due to an unknown error.'
  • How to set default security access for a Page Type in Silverstripe?
  • Get Users in Group from Azure AD via Microsoft Graph
  • Maven, ActionBarSherlock v4 and Roboguice v2 - How do I get it to build?
  • Is Android development possible on netbooks? [closed]
  • How can I create one checksum of two files in Maven to use it in the name of a directory
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Classic ASP URL Rewriting
  • Efficient User-Agent Regex to find Safari in Python
  • Most efficient way to move table rows from one table to another
  • Java color detection
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • OOP Javascript - Is “get property” method necessary?
  • Functions in global context
  • Scrapy recursive link crawler
  • Java applet as stand-alone Windows application?
  • Delete MySQLi record without showing the id in the URL
  • XCode can't find symbols for a specific iOS library/framework project
  • Unanticipated behavior
  • Comma separated Values
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app