mysql對執行結果進行html格式的輸出?輸出html格式?


需求描述:

  在執行mysql命令的時候,有的時候需要將查詢的結果輸出到文件,如果想要html格式的,應該怎么輸出,

  在此記錄下操作的過程.

1.通過tee命令結合--html輸出查詢結果到html文件

[mysql@testvm ~]$ mysql --html   #--html選項的意思產生html格式的輸出.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.21-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> tee test.html    #tee命令表示將之后的內容記錄到某個文件
Logging to file 'test.html'
mysql> select * from test.tab_json;    #由於使用--html選項,輸出的查詢結果都是html格式 <TABLE BORDER=1><TR><TH>id</TH><TH>uid</TH><TH>info</TH></TR><TR><TD>1</TD><TD>1001</TD><TD>{&quot;name&quot;: &quot;????&quot;, &quot;status&quot;: 0, &quot;addtime&quot;: &quot;2017-10-10&quot;}</TD></TR><TR><TD>2</TD><TD>1002</TD><TD>[{&quot;name&quot;: &quot;????&quot;, &quot;status&quot;: 0, &quot;addtime&quot;: &quot;2017-10-11&quot;}, {&quot;name&quot;: &quot;????&quot;, &quot;status&quot;: 0, &quot;addtime&quot;: &quot;2017-10-12&quot;}]</TD></TR><TR><TD>3</TD><TD>1003</TD><TD>[{&quot;name&quot;: &quot;????&quot;, &quot;status&quot;: 0, &quot;addtime&quot;: &quot;2017-10-12&quot;}, {&quot;name&quot;: &quot;????&quot;, &quot;status&quot;: 0, &quot;addtime&quot;: &quot;2017-09-28&quot;}]</TD></TR></TABLE>3 rows in set (0.00 sec)

mysql> notee;   #關閉記錄日志.
Outfile disabled.

2.查看生成的文件及內容

[mysql@testvm ~]$ ls -ltr
total 12
drwxrwxr-x 2 mysql mysql 4096 Jun  7 15:39 workspace
-rw-rw-r-- 1 mysql mysql  720 Jul 17 13:30 html_test.html
-rw-rw-r-- 1 mysql mysql  796 Jul 17 13:48 test.html

 

3.如果不想要看到查詢語句,可以在shell命令行中執行

[mysql@testvm ~]$ mysql --html -e "select * from test.tab_json;"  > html_test.html   #注意是重定向符號>
[mysql@testvm ~]$ ls -ltr
total 12
drwxrwxr-x 2 mysql mysql 4096 Jun  7 15:39 workspace
-rw-rw-r-- 1 mysql mysql  796 Jul 17 13:48 test.html
-rw-rw-r-- 1 mysql mysql  720 Jul 17 13:53 html_test.html

  4.查看文件內容

備注:輸出的文件中就不包括查詢語句和查詢了多少行的信息.

 5.如果不想要列標題使用-N選項

[mysql@testvm ~]$ mysql --html -N -e "select * from test.tab_json;"  > html_test.html

 

 

 備注:已經輸出了html格式的文件,並且不帶列標題.

 

小結:

  • 通過--html將查詢結果以html格式輸出,對其他的查詢沒有影響
  • 命令行中執行,使用>進行重定向輸出,輸出的結果中沒有查詢語句本身

 

文檔創建時間:2018年7月17日13:58:55


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM