简体   繁体   中英

mysql dump from specific tables that relate to each other

I have 2 tables in my MySQL database.

I need to output both tables to SQL dump files. (CREATE scripts)

However, I may only dump certain rows. (eg where column = value).

This is fine for the first table.

However, the second table may only dump rows where eg column = value that exists in 1st result set.

I wanted to use mysqldump, however I don't think it will work for this type of query unless I do some sort of join or union ?

I can also use PHP and create the file while looping through the rows, however I don't think that is the most efficient way.

Any advise or help will be appreciated !

Thank you @MarcB for answering the question, it worked 100%. I am posting this to follow stack overflow rules of answering and also to show what I ended up using.

So here is the 2 mysqldump commands I run inside of a bash script : (I replaced some private data with 'x')

#!/bin/bash

#dump the first table
mysqldump -h xxx.xxx.xxx.xxx -u xxxxx -pxxxxx --where="name='value'" databasename table1 > /home/xxxxx/xxxxx/table1.sql

#dump the second table that relates to the first one
mysqldump -h xxx.xxx.xxx.xxx -u xxxxx -pxxxxx --lock-all-tables --where="field_id in (select anotherfield_id from table1 where name='value')" databasename table2 > /home/xxxxx/xxxxx/table2.sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM