是否有一个简单的CLI命令可以将SELECT语句的速度复制到MySQL中的文本文件(没有数据)?

时间:2017-11-17 17:11:39

标签: mysql command-line-interface

让我说我跑:

CREATE TABLE t1 AS
SELECT ssn, title
FROM employees
WHERE title = "Dictator"

我想自动保存到文本文件中:

Query OK, 1118933 rows affected (3.84 sec)
Records: 1118933  Duplicates: 0  Warnings: 0

如果您还可以将查询保存在其上方,则可获得积分:)!

CREATE TABLE t1 AS
SELECT ssn, title
FROM employees
WHERE title = "Dictator"

Query OK, 1118933 rows affected (3.84 sec)
Records: 1118933  Duplicates: 0  Warnings: 0

2 个答案:

答案 0 :(得分:2)

您可以在mysql客户端中使用tee builtin命令。

我在MacOS上测试了这个:

mysql> tee out
Logging to file 'out'

mysql> create table test.mytable as select sleep(2);
Query OK, 1 row affected (2.09 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> exit;
Bye

$ cat out
mysql> create table test.mytable as select sleep(2);
Query OK, 1 row affected (2.09 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> 

答案 1 :(得分:1)

  

在这种情况下MacOS。但是,我也很想知道其他人   系统也是如此。可能会使社区受益。

tee命令也适用于Windows。
请注意,您需要使用完整路径并且需要创建文件

mysql> tee C:/out.txt
Logging to file 'C:/out.txt'
mysql> select * from stackoverflow.newtable;
+---------+---------+---------+
| Column1 | Column2 | Column3 |
+---------+---------+---------+
| Data1   | Data3   | Data5   |
| Data2   | Data4   | Data6   |
| 1       | 2       | NULL    |
| 1       | 2       | NULL    |
| 2       | 1       | NULL    |
+---------+---------+---------+
5 rows in set (0.00 sec)

内容关闭C:/out.txt

mysql> select * from stackoverflow.newtable;
+---------+---------+---------+
| Column1 | Column2 | Column3 |
+---------+---------+---------+
| Data1   | Data3   | Data5   |
| Data2   | Data4   | Data6   |
| 1       | 2       | NULL    |
| 1       | 2       | NULL    |
| 2       | 1       | NULL    |
+---------+---------+---------+
5 rows in set (0.00 sec)