我有以下数字:
0,37,38,39,40,43,54,55,56,57,60,67,68,71,76,79,81,84,91,97,105,111,116,117,37,38,39,40 ,43,54,55,56,57,60,67,68,71,76,79,81,84,88,91,97,105,111,116,117,118,119,37,38,39,40,43,54,55,56,57 ,60,67,68,71,76,79,81,84,88,91,97,105,111,116,117,120,121,124,130,139,143,151,155,157,167,172,182,187,189,190,195,200,201,227,228,230,232,233,234,235,236,238,239,240,241,242,243,244,246,247,281,282,283,284,285,286,287,289,290,316,331,341,342,343,344,351,369,370,396,399,403,404,405,406,407
我想知道有多少号码。例如......
SELECT COUNT (11,12,13,14,15,16,17,18,19,20)
...应该返回10个数字。有没有办法使用MySQL做到这一点?
答案 0 :(得分:3)
使用length
和replace
功能的组合。
mysql> SELECT length(replace('1,2,3,4,5,6,7,8,9,0', ',', '' ));
+--------------------------------------------------+
| length(replace('1,2,3,4,5,6,7,8,9,0', ',', '' )) |
+--------------------------------------------------+
| 10 |
+--------------------------------------------------+
1 row in set (0.33 sec)
然而,你必须处理字符串中存在的其他字符。
我还希望你要求的是数字位数而不是字符串中CSV的数量。
答案 1 :(得分:3)
实际上我认为你真的在寻找:
SELECT length('43,54,55,56,57,60,67,68,71') -
length(replace('43,54,55,56,57,60,67,68,71', ',' ,'')) + 1
以上内容将返回9
。
如果我要使用
SELECT length(replace('43,54,55,56,57,60,67,68,71', ',', '' ));
它会返回18
这是不正确的,除非你在逗号以外的唯一数量的字符之后。