简体   繁体   English

我可以在mysqldump中使用查询吗?

[英]Can i use queries inside mysqldump?

I have a Mysql database on my remote server with several tables. 我的远程服务器上有一个带有多个表的Mysql数据库。 I need to get some of this tables with some of its rows using PHP to a local WPF program which uses SQLite. 我需要使用PHP到使用SQLite的本地WPF程序来获取某些表及其某些行。

The rows i need to get from each table depends of some different values for each table (i mean i must apply a different WHERE clause to every table), and i need to be able to use standar MySQL operations (like LEFT JOIN, UNION, etc). 我需要从每个表获取的行取决于每个表的一些不同值(我的意思是我必须对每个表应用不同的WHERE子句),并且我需要能够使用标准MySQL操作(如LEFT JOIN,UNION,等等)。

So far, i am able to dump just 1 table with 1 WHERE clause: 到目前为止,我只能使用1个WHERE子句转储1个表:

exec('./mysql2sqlite.sh --databases test -u test -pTest --tables users --where "a="' . $a . '" AND b="' . $b . ' | sqlite3 db/file.sqlite');

I think what i need is to be able to execute several queries and then dump all the results inside the mysqldump, is it possible? 我认为我需要能够执行几个查询,然后将所有结果转储到mysqldump中,这可能吗?

Also, is this the right approach to solve this kind of problem? 另外,这是解决此类问题的正确方法吗? Or should i dump the format of the desire tables first and then dump the desire rows? 还是我应该先转储期望表的格式,然后再转储期望行?

PD: I am using this MySQL2SQLite converter. PD:我正在使用这个MySQL2SQLite转换器。

I would solve the problem by creating a separate temporary database called something like tmp_backup , creating tables that would map your tables but only include the desired rows, and then performing a dump of that database. 我将通过创建一个名为tmp_backup类的单独的临时数据库来解决该问题,创建将映射表但仅包含所需行的表,然后执行该数据库的转储。 Example of a temporary database table: 临时数据库表的示例:

CREATE tmp_backup.customers LIKE real_db.customers;
SELECT c.* FROM real_db.customers  c 
LEFT JOIN real_db.bad_customers o USING(customer_id) WHERE o.customer_id IS NULL

If the tables are big, you could also use views, but you will need to patch mysqldump as it will dump views as views, not as tables. 如果表很大,您也可以使用视图,但是您将需要修补mysqldump因为它将视图以视图而不是表的形式转储。 Look for 寻找

if (strcmp(table_type, "VIEW") == 0)
    DBUG_VOID_RETURN;

in client/mysqldump.c in the MySQL source, comment it out and recompile. 在MySQL源代码的client/mysqldump.c中,将其注释掉并重新编译。 You will find the new binary for mysqldump in the client/ directory and you can just copy into some directory under another name without a full install. 您将在client/目录中找到mysqldump的新二进制文件,而无需完全安装即可直接复制到其他名称的某个目录中。 This hack should work, although I have not yet verified it - will post an update when I do. 尽管我尚未验证此hack,但它应该可以工作-当我这样做时,它将发布更新。

UPDATE - the above hack for mysqldump does work with views - I tested it on 5.5.35, but it should work the same with newer 5.5 and likely 5.6/5.7 versions. 更新-以上针对mysqldump hack确实适用于视图-我在5.5.35上对其进行了测试,但它应与更新的5.5以及可能的5.6 / 5.7版本相同。 After the patch, you do need to give it -t argument to just get the table data with the definitions. 补丁之后,您确实需要给它-t参数以仅获取具有定义的表数据。 So you will do mysqldump --no-data real_db first to get the definitions, and then mysqldump -t tmp_backup to get the view data. 因此,您将首先执行mysqldump --no-data real_db获取定义,然后执行mysqldump -t tmp_backup获取视图数据。 Then you can just load the in order (definitions first, then the data) with mysql utility. 然后,您可以使用mysql实用程序按顺序加载(先定义,然后是数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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