`psql` 扩展模式与 `mysql` 等效

`psql` 扩展模式与 `mysql` 等效

因以下事情而感到沮丧:

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

答案1

如果我正确理解了您想要实现的目标,您可以尝试使用 \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

相关内容