且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mySql:获取单元格中的单词数(以逗号分隔的字符串)与匹配单元格具有相同值的行

更新时间:2023-11-15 16:08:46

考虑:

select *
from tab1
where char_length(type) - char_length(replace(type, ',', '')) + 1 = qta

表达式char_length(type) - char_length(replace(type, ',', ''))为您提供字符串中的逗号数.在其中添加1可以得到字符串中的单词数.

Expression char_length(type) - char_length(replace(type, ',', '')) gives you the number of commas in the string. Adding 1 to that gives you the number of words in the string.

DB Fiddle上的演示 :

Demo on DB Fiddle:


id | cod | type        | qta
-: | :-- | :---------- | --:
 1 | aaa | aaa,bbb,ccc |   3
 2 | aaa | ddd         |   1
 4 | aaa | ggg,hhh     |   2