Mysql

mysqlpsql擴展模式等效性

  • August 7, 2014

我對以下事情感到沮喪:

0:33:1407402356:root@ahost:~# echo 'use wordpress_3_6_1; select * from wp_posts;'
| mysql -u mysqluser -pmysqlpasswdord | wc -l -L
42   40585

SQL 查詢結果被破壞得很醜。

PostgreSQLpsql提供了輔助功能擴展模式。看看它的實際效果:

postgres@ahost:~$ echo '\c openerp7-0 \\ select * from pg_shadow' | psql
You are now connected to database "openerp7-0" as user "postgres".
usename  | usesysid | usecreatedb | usesuper | usecatupd | userepl |          
  passwd                | valuntil | useconfig 
----------+----------+-------------+----------+-----------+---------+----------
-------------------------+----------+-----------
openerp |    16384 | t           | t        | t         | t       | 
ahash |          | 
postgres |       10 | t           | t        | t         | t       | 
anotherhash |          | 
(2 rows)

postgres@ahost:~$ echo '\c openerp7-0 \\ \x \\ select * from pg_shadow' |
psql                
You are now connected to database "openerp7-0" as user "postgres".
Expanded display is on.
-[ RECORD 1 ]------------------------------------
usename     | openerp
usesysid    | 16384
usecreatedb | t
usesuper    | t
usecatupd   | t
userepl     | t
passwd      | ahash
valuntil    | 
useconfig   | 
-[ RECORD 2 ]------------------------------------
usename     | postgres
usesysid    | 10
usecreatedb | t
usesuper    | t
usecatupd   | t
userepl     | t
passwd      | anotherhash
valuntil    | 
useconfig   |

這種擴展模式對於有很多列的表來說很可愛:

postgres@ahost:~$ echo '\c openerp7-0 \\ \x \\ select * from res_partner' |
psql | wc -l -L
223   44423
postgres@ahost:~$ echo '\c openerp7-0 \\ select * from res_partner' | psql |
wc -l -L
 9   94030

當然,只有當人們不介意用wc -l.

人們如何才能完成mysql類似於 中的擴展模式的功能psql

如果我正確理解您想要實現的目標,您可以嘗試使用 \G 分隔符(而不是分號),如下所示:

echo 'SELECT * FROM mytable\G' | mysql -u myuser -p mypassword mydb

範例輸出:

*************************** 1. row ***************************
   id: 1
locale: de
 name: Afghanistan
*************************** 2. row ***************************
   id: 2
locale: de
 name: Ägypten

引用自:https://serverfault.com/questions/618642