如何解决总和,长度和分组依据的问题

时间:2019-07-16 05:37:06

标签: mysql sql

字段合同是文本,值是用“,”分隔的字符串。

查询:

select sum( if( length(dapps.contracts) = 
       sum(length(replace(dapps.contracts,',',''))) , 1 , length(dapps.contracts) 
     - sum(length(replace(dapps.contracts,',',''))) ) ) as f1 
  from dapps 
 group by id 

显示:

Error in query (1111): Invalid use of group function 

需要一级查询

此查询工作正常

select sum(f1)
from (
 select if( length(dapps.contracts) = 
        sum(length(replace(dapps.contracts,',',''))) , 1 , length(dapps.contracts) 
      - sum(length(replace(dapps.contracts,',',''))) ) as f1 
   from dapps 
  group by id
) tb1​

需要查询而无需子查询

0 个答案:

没有答案