在String中计算大写字母

时间:2011-04-14 10:13:04

标签: mysql sql

如何使用MySQL查询计算大写字母? 我正在尝试

COUNT(IF(MATCH(post_text) AGAINST('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'),1,NULL))

但是这给了我一个错误的说法:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U' at line 4

我想,我不允许不在禁用

的AGAINST条款中使用列表

那么,有没有办法实现这个目标?

5 个答案:

答案 0 :(得分:2)

尝试此功能 -

DELIMITER $$

CREATE FUNCTION cnt_up_case(str VARCHAR(255))
RETURNS VARCHAR(255)
NOT DETERMINISTIC
CONTAINS SQL
BEGIN

  SET @cnt = 0;
  SET @len = length(str);
  SET @i = 1;

  WHILE @i <= @len
  DO
    SET @c = substring(str, @i, 1);
    IF ascii(@c) > 64 AND ascii(@c) < 91 THEN
      SET @cnt = @cnt + 1;
    END IF;

    SET @i = @i + 1;
  END WHILE;

  RETURN @cnt;
END
$$

DELIMITER ;

示例:

SET @Param1 = 'AbCdE';
SET @ResultValue = cnt_up_case(@Param1);
SELECT @ResultValue;
--------------
3

答案 1 :(得分:1)

如果您的表有一列(列名)并且具有以下值:

  • AAAAAA
  • AAAAAA
  • AAAAAA

使用以下查询获得每个值的资本计数:

SELECT columnname,(length(columnname)  - length( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(naam,'Z','') ,'Y','') ,'X','') ,'W','') ,'V','') ,'U','') ,'T','') ,'S','') ,'R','') ,'Q','') ,'P','') ,'O','') ,'M','') ,'M','') ,'L','') ,'K','') ,'J','') ,'I','') ,'H','') ,'G','') ,'F','') ,'E','') ,'D','') ,'C','') ,'B','') ,'A','') ) )  FROM column

答案 2 :(得分:0)

你在角色A之前缺少第一个,在Z之后缺少一个。

答案 3 :(得分:0)

只是一个想法 - 你在开始尝试使用SELECT语句吗?

SELECT COUNT(IF(MATCH(post_text) AGAINST('A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'),1,NULL))

答案 4 :(得分:0)

我有个主意,但很脏......

您可以从post_text中删除大写字母,然后将其长度与原始post_text长度进行比较:

COUNT(post_text) - COUNT( REPLACE(REPLACE(REPLACE(REPLACE(post_text, 'D', ''), 'C', ''), 'B', ''), 'A', '') )

我不知道你是否想要使用它,我不想让你相信它,但它是其中一种可能性。