免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9878 | 回复: 10
打印 上一主题 下一主题

mysql中如何识别记录中的tab键,回车键? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-16 16:28 |只看该作者 |倒序浏览
mysql中如何识别记录中的tab键,回车键?

比如下列code a到where之间,应该就有个tab键或回车键:
+-------------------------------------------------------+
| trim(sql_text)                                                |
+-------------------------------------------------------+
| select count(*) from code a               where |
| status <> 'D'                                                 |
+-------------------------------------------------------+

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
2 [报告]
发表于 2009-03-16 16:36 |只看该作者

回复 #1 xzh2000 的帖子

好像不能,没有这样的函数。

你的目的是啥?

论坛徽章:
0
3 [报告]
发表于 2009-03-16 17:00 |只看该作者
原帖由 枫影谁用了 于 2009-3-16 16:36 发表
好像不能,没有这样的函数。

你的目的是啥?


我主要是想把SQL中多余的空格与控制符去掉...

论坛徽章:
0
4 [报告]
发表于 2009-03-16 17:05 |只看该作者
偶已经搞定了:

delimiter //
drop function if exists sql_clearspace//

create function sql_clearspace(v_sql varchar(4000))
  returns varchar(4000)
begin
  declare v_tmps varchar(4000) default '';
  declare v_last varchar(2) default '';
  declare v_curr varchar(2) default '';
  declare v_num int default 0;
  declare v_max int default 0;
  
  set v_max = length(v_sql);
  while v_num <> v_max do
    set v_num = v_num + 1;
    set v_curr = substr(v_sql, v_num, 1);
   
    if (v_curr = char(9) or v_curr = char(10)) then
      set v_curr = char(32);
    end if;
   
    if (v_curr <> char(32) and (v_last is null or v_last <> char(32))) then
      set v_tmps = concat(v_tmps, v_curr);
      set v_last = v_curr;
    elseif (v_last <> char(32) and v_curr = char(32)) then
      set v_tmps = concat(v_tmps, v_curr);
      set v_last = v_curr;
    elseif (v_last = char(32) and v_curr <> char(32)) then
      set v_tmps = concat(v_tmps, v_curr);
      set v_last = v_curr;
    end if;
  end while;
  
  return v_tmps;
end//

delimiter ;


mysql> select sql_clearspace('select count(*) from code a                     where status <> 1');
+-------------------------------------------------------------------------------------+
| sql_clearspace('select count(*) from code a                     where status <> 1') |
+-------------------------------------------------------------------------------------+
| select count(*) from code a where status <> 1                                       |
+-------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
5 [报告]
发表于 2009-03-16 17:15 |只看该作者
mysql> select replace("select count(*) from code a               where",'  ','');  
+--------------------------------------------------------------------+
| replace("select count(*) from code a               where",'  ','') |
+--------------------------------------------------------------------+
| select count(*) from code a where                                  |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>


可以这样。。。

论坛徽章:
0
6 [报告]
发表于 2009-03-16 17:20 |只看该作者
原帖由 枫影谁用了 于 2009-3-16 17:15 发表
mysql> select replace("select count(*) from code a               where",'  ','');  
+--------------------------------------------------------------------+
| replace("select count(*) from code a ...


谢谢...

论坛徽章:
0
7 [报告]
发表于 2009-03-16 17:23 |只看该作者
原帖由 枫影谁用了 于 2009-3-16 17:15 发表
mysql> select replace("select count(*) from code a               where",'  ','');  
+--------------------------------------------------------------------+
| replace("select count(*) from code a ...


测试了一下,发现这个还没有考虑换行符:
mysql> select replace("select count(*)   from code a               
    ">       where status <> 1",'  ','');
+-----------------------------------------------------------------------------------------+
| replace("select count(*)   from code a               
      where status <> 1",'  ','') |
+-----------------------------------------------------------------------------------------+
| select count(*) from code a
where status <> 1                                          |
+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
8 [报告]
发表于 2009-03-16 17:59 |只看该作者

回复 #7 xzh2000 的帖子

mysql> select replace("select count(*) from code a               where                                            dsdsds                                                                                                            ",'\n','');
+----------------------------------------------------------------------------+
| replace("select count(*) from code a               where
dsdsds
",'\n','') |
+----------------------------------------------------------------------------+
| select count(*) from code a               wheredsdsds                      |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
9 [报告]
发表于 2009-03-16 18:06 |只看该作者
mysql> select @a;
+------------------------------------------------------------+
| @a                                                         |
+------------------------------------------------------------+
| select count(*) from code a               where
abc  <> 0 |
+------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select replace(replace(@a,'n',''),'  ','');
+-------------------------------------------+
| replace(replace(@a,'n',''),'  ','')      |
+-------------------------------------------+
| select count(*) from code a where abc<> 0 |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql>


这个结果是你要的吧!

论坛徽章:
0
10 [报告]
发表于 2009-03-16 18:09 |只看该作者
原帖由 枫影谁用了 于 2009-3-16 18:06 发表
mysql> select @a;
+------------------------------------------------------------+
| @a                                                         |
+----------------------------------------------- ...


差不多了,谢谢...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP