mysql 的 concat 组合的妙用

来源:互联网 发布:八爪鱼设置数据库 编辑:程序博客网 时间:2024/06/04 20:07

数据库里面有很多视图过期,需要删除,就慢慢手工删除,老大考到后,就交给我一个用concat 组合批量删除,很是迷茫,后来经老大讲解,查资料,终于发现concat的强大功能,拿来于大家分享。

MySQL中concat函数
使用方法:
CONCAT(str1,str2,…)

返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。

注意:
如果所有参数均为非二进制字符串,则结果为非二进制字符串。
如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。
一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如:
SELECT CONCAT(CAST(int_col AS CHAR), char_col)

MySQL的concat函数可以连接一个或者多个字符串,如
mysql> select concat('10');
+--------------+
| concat('10') |
+--------------+
| 10 |
+--------------+
1 row in set (0.00 sec)

mysql> select concat('11','22','33');
+------------------------+
| concat('11','22','33') |
+------------------------+
| 112233 |
+------------------------+
1 row in set (0.00 sec)

MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
mysql> select concat('11','22',null);
+------------------------+
| concat('11','22',null) |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)

 

实例应用:用concat组合,删除相关的视图语句
mysql>select concat('drop view',' ', table_name,';') from information_schema.tables where table_schema ='db_sys_stat' and table_type='view';
+-----------------------------------------+
| concat('drop view',' ', table_name,';') |
+-----------------------------------------+
| drop view v_asteriskcdrdb_stat;         |
| drop view v_ctidb_stat;                 |
| drop view v_gwsyj_stat;                 |
| drop view v_iphoneyj_stat;              |
| drop view v_man875_stat;                |
| drop view v_new_875_cn_stat;            |
| drop view v_oms_stat;                   |
| drop view v_onlineschool_stat;          |
| drop view v_q2a_stat;                   |
| drop view v_semsyj_stat;                |
| drop view v_shangjisousuo_stat;         |
| drop view v_shengyijie_db_stat;         |
| drop view v_syjcms_stat;                |
| drop view v_tstat_stat;                 |
| drop view v_wapsyj_stat;                |
| drop view v_www_new875_stat;            |
| drop view v_www_tlkj_com_stat;          |
| drop view v_yehehedb_stat;              |
| drop view v_zhaoshangyi_stat;           |
| drop view v_zsyblog_stat;               |
+-----------------------------------------+

原创粉丝点击