Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL export query as insert statements

Tags:

mysql

export

I have two mysql tables which both have a typeID in common. I am wanting to select everything from these two tables with the same typeID, query below:

SELECT ta.requiredTypeID, ta.typeID, ta.quantity 
  FROM `invtypes` as t, `typeactivitymaterials` as ta 
  WHERE volume > 0 AND t.typeID = ta.typeID;

This gives me the correct results but I am trying to export this query as insert statements. I have tried adding INTO OUTFILE "/path/" but this only exports the data as tab/comma delimited data, is it possible to export this data as insert statements?

Cheers

Eef

like image 275
RailsSon Avatar asked Sep 24 '10 12:09

RailsSon


People also ask

How do I export SQL data to insert statements?

Right click database >> Tasks >> Generate Scripts. Choose Objects: Select specific database objects >> Select the table you want to export. Set scripting options: Select Save to file then click Advanced. Under advanced >> General Options >> Types of data to script - choose Data only >> Click OK.

How do I export inserts from SQL Workbench?

Open MySQL Workbench > Home > Manage Import / Export (Right bottom) / Select Required DB > Advance Exports Options Tab >Complete Insert [Checked] > Start Export. For 6.1 and beyond, thanks to ryandlf: Click the management tab (beside schemas) and choose Data Export.

How do I export the insert script from toad?

you can generate insert script using toad,write table name in toad and press F4 then right click on table name and select export data and then new window will open ,select export format as insert statement then file path to save,script will be generated at specified path.....


3 Answers

You might be able to use mysqldump instead. Check out the parameters --tables and --where=.

http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html

like image 165
a'r Avatar answered Sep 29 '22 17:09

a'r


If you have MySQL Workbench it can generate insert statements for you.

In the SQL editor run your query, then from the result set you can click the export button. Then in the "Save as Type" select as "SQL INSERT statements (*.sql)". Click Save, confirm the table you are exporting and click ok.

If you open the file you should see your result set as insert statements with the column names.

I only tested this on a simple select * from table. I haven't tested this with multiple tables. I hope this helps.

EDIT: Seems like workbench is available for Windows, OSX, and Linux (Thanks vcardillo)

like image 26
Yodacheese Avatar answered Sep 29 '22 16:09

Yodacheese


You can combine INSERT with SELECT to insert records directly from the result of a select statement.

The way you do it would be something like this:

INSERT INTO newtable (requiredTypeID, typeID, quantity)
     SELECT ta.requiredTypeID, ta.typeID, ta.quantity 
         FROM `invtypes` as t, `typeactivitymaterials` as ta 
         WHERE volume > 0 AND t.typeID = ta.typeID;

Here's a link to the relevant MySQL manual page.

Note that this would actually do the insert right away; it wouldn't do quite what you asked (which is to generate the insert statements for use later on).

like image 28
Spudley Avatar answered Sep 29 '22 16:09

Spudley