Mysql 跨多个sql列计算字符串出现次数
如何计算表中出现的$符号总数?简单的选择查询在这种情况下不起作用。如何编辑select查询以计算表中多个列中的字符串出现次数 c1 | c2 | c3 | c4 | c5| $ | | | $ | | | $ | $ | $ | | | | | | | $ | | | | | c1 | c2 | c3 | c4 | c5| $ | | | $ | | | $ | $ | $ | | | | | | | $ | | | | | ($符号在表中任意列中随机出现)Mysql 跨多个sql列计算字符串出现次数,mysql,postgresql,Mysql,Postgresql,如何计算表中出现的$符号总数?简单的选择查询在这种情况下不起作用。如何编辑select查询以计算表中多个列中的字符串出现次数 c1 | c2 | c3 | c4 | c5| $ | | | $ | | | $ | $ | $ | | | | | | | $ | | | | | c1 | c2 | c3 | c4 | c5| $ | | | $ | | | $ | $ |
我试图通过正则表达式获取计数,但它总是返回存在$的行数,而不是$数。您可以使用
case
inside count来计算列中出现的$
select
count(case when c1 = '$' then 1 else null end)
+ count(case when c2 = '$' then 1 else null end)
+ count(case when c3 = '$' then 1 else null end)
+ count(case when c4 = '$' then 1 else null end)
+ count(case when c5 = '$' then 1 else null end) cnt
from your_table
您可以使用
case
inside count来计算列中出现的$
select
count(case when c1 = '$' then 1 else null end)
+ count(case when c2 = '$' then 1 else null end)
+ count(case when c3 = '$' then 1 else null end)
+ count(case when c4 = '$' then 1 else null end)
+ count(case when c5 = '$' then 1 else null end) cnt
from your_table
不需要正则表达式。这是关于PostgreSQL还是MySQL的?是的,欢迎使用postgresSQL或MySQL查询。您正在尝试透视数据。MySQL没有pivot函数。您必须使用带有count的case语句。不需要正则表达式。这是关于PostgreSQL还是MySQL的?是的,欢迎使用postgresSQL或MySQL查询。您正在尝试透视数据。MySQL没有pivot函数。您必须使用count.M khalid Junaid的用例语句,它成功了,谢谢!:)M khalid Junaid,成功了,谢谢!:)