被以下事情挫败:
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查询结果变得丑陋。
PostgreSQL的psql提供了辅助function扩展模式 。 看到它在行动:
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来改变行数的时候。
人们如何完成一个类似于psql扩展模式的function?
如果我正确理解你想达到的目的,你可以用\ 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