MySQL輸出html格式檔案

lhrbest發表於2019-07-08

MySQL輸出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 13Server 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 12drwxrwxr-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 12drwxrwxr-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格式輸出,對其他的查詢沒有影響
  • 命令列中執行,使用>進行重定向輸出,輸出的結果中沒有查詢語句本身






About Me

........................................................................................................................

● 本文作者:小麥苗,部分內容整理自網路,若有侵權請聯絡小麥苗刪除

● 本文在itpub( http://blog.itpub.net/26736162 )、部落格園( http://www.cnblogs.com/lhrbest )和個人weixin公眾號( xiaomaimiaolhr )上有同步更新

● 本文itpub地址: http://blog.itpub.net/26736162

● 本文部落格園地址: http://www.cnblogs.com/lhrbest

● 本文pdf版、個人簡介及小麥苗雲盤地址: http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答: http://blog.itpub.net/26736162/viewspace-2134706/

● DBA寶典今日頭條號地址: http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

........................................................................................................................

● QQ群號: 230161599 (滿) 、618766405

● weixin群:可加我weixin,我拉大家進群,非誠勿擾

● 聯絡我請加QQ好友 646634621 ,註明新增緣由

● 於 2019-07-01 06:00 ~ 2019-07-31 24:00 在西安完成

● 最新修改時間:2019-07-01 06:00 ~ 2019-07-31 24:00

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

........................................................................................................................

小麥苗的微店 https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

小麥苗出版的資料庫類叢書 http://blog.itpub.net/26736162/viewspace-2142121/

小麥苗OCP、OCM、高可用網路班 http://blog.itpub.net/26736162/viewspace-2148098/

小麥苗騰訊課堂主頁 https://lhr.ke.qq.com/

........................................................................................................................

使用 weixin客戶端 掃描下面的二維碼來關注小麥苗的weixin公眾號( xiaomaimiaolhr )及QQ群(DBA寶典)、新增小麥苗weixin, 學習最實用的資料庫技術。

........................................................................................................................

歡迎與我聯絡

 

 



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2649799/,如需轉載,請註明出處,否則將追究法律責任。

相關文章