Export select records from a SQL table

I need to export certain records from a table and then import them into another table.

SELECT * FROM `survey_data` WHERE `UPDATE_DAT` < "20161001"

These are the records that I want.

How do I import them into the 'history' table?

Glenn
breeze351Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Olaf DoschkeSoftware DeveloperCommented:
Depends. Assumed the history table has same structure as `survey_data`, then it can be as simple as

INSERT INTO `history` SELECT * FROM `survey_data` WHERE `UPDATE_DAT` < '20161001' 

Open in new window


Will also depend on your database.

Bye, Olaf.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pawan KumarDatabase ExpertCommented:
>> Is your `history`table is in the same database as `survey_data`???, If yes

If the table already exists you can use below .. If you need you can also specify columns if both the tables

INSERT INTO `history` (col1,col2,col3...)
SELECT col1,col2,col3.... FROM `survey_data` WHERE `UPDATE_DAT` < "20161001"

Open in new window

Read more about Insert - SELECT from mySQL - https://dev.mysql.com/doc/refman/5.5/en/insert-select.html

Is your `history`table is in the same database as `survey_data`???, If No

Then we can use mysqldump command. Once the dump is done then you can delete the extra records from the history table

mysqldump --no-create-info --no-create-db --user=userName --password=yourpassword DBName `survey_data` \
| sed -e 's/`survey_data`/`history`/' \
| mysql --user=username --password=pwd `history`

Open in new window

0
breeze351Author Commented:
That works, structure is the same.
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.