mysql concat例子_巧用MySQL的CONCAT函数

在日常工作中经常会遇到领导让你查一下线上数据库占用多少空间啦.统计一下各个表的数据量啦.等等需求.

那么今天我们就以统计各表的数据量为题看看如何才能快速完成这种需求呢?

这里用到的函数是CONCAT函数

举个例子简单看一下如何使用

mysql> select concat('a','b','c'),concat('d',null);

+---------------------+------------------+

| concat('a','b','c') | concat('d',null) |

+---------------------+------------------+

| abc                 | NULL             |

+---------------------+------------------+

1 row in set (0.00 sec)

那么我们需要统计每个表的数据量用CONCAT函数形成批量脚本SQL语句如下:

mysql>select concat('select ','\'',table_name,'\',''\,\'',',count(*) from ',table_name,'; ') from information_schema.tables where table_schema='mysql';

结果如下:

+----------------------------------------------------------------------------------+

| concat('select ','\'',table_name,'\',''\,\'',',count(*) from ',table_name,'; ')  |

+----------------------------------------------------------------------------------+

| select 'columns_priv',',',count(*) from columns_priv;                            |

| select 'db',',',count(*) from db;                                                |

| select 'event',',',count(*) from event;                                          |

| select 'func',',',count(*) from func;                                            |

| select 'general_log',',',count(*) from general_log;                              |

| select 'help_category',',',count(*) from help_category;                          |

| select 'help_keyword',',',count(*) from help_keyword;                            |

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

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

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

我们如何去掉讨厌的MySQL显示边框呢?

[root@localhost ]$ mysql -u root -pxxxxx -S/tmp/mysql.sock -N -s -e "select concat('select ','\'',table_name,'\',''\,\'',',count(*) from ',table_name,'; ') from information_schema.tables where table_schema='mysql';" select 'columns_priv',',',count(*) from columns_priv;

select 'db',',',count(*) from db;

select 'event',',',count(*) from event;

select 'func',',',count(*) from func;

select 'general_log',',',count(*) from general_log;

select 'help_category',',',count(*) from help_category;

select 'help_keyword',',',count(*) from help_keyword;

select 'help_relation',',',count(*) from help_relation;

select 'help_topic',',',count(*) from help_topic;

select 'host',',',count(*) from host;

select 'ndb_binlog_index',',',count(*) from ndb_binlog_index;

select 'plugin',',',count(*) from plugin;

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

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

这样就可以了.然后直接保存成脚本source一下各表的数据就出来.保存成CSV格式用Excel排序一下就可以交差了.^_^

MySQL用的参数

-N, --skip-column-names   Don't write column names in results.

-s, --silent     Be more silent. Print results with a tab as separator, each row on new line.

-e, --execute=name   Execute command and quit. (Disables --force and history file.)

转载请注明出处.谢谢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值