Php 如何使用sql获取多列的计数

Php 如何使用sql获取多列的计数,php,mysql,sql,Php,Mysql,Sql,我需要把身份证号码和证书号码从1数到10 My table like --------------- trtm t_date id_no certno1 certno2 certno3 certno4 certno5 certno6 certno7 certno8 certno9 certno10 TM 15/02/2002 A12 2158 TM 1

我需要把身份证号码和证书号码从1数到10

   My table like
   ---------------
      trtm       t_date          id_no  certno1   certno2  certno3  certno4  certno5  certno6 certno7 certno8 certno9  certno10 
       TM       15/02/2002       A12      2158
       TM       15/02/2010       A13      8181     8182     8183     8184     8185     8186     8187     8188      8189     8190
       TM       15/02/2010       A14      19138
      -------------------
但我需要证书的计数,其中值是3,但我需要的是12

   my query is
   ----------
  select count(id_no) as total_id,count(CONCAT(certno1, certno2, certno3,certno4,certno5,certno6,certno7,certno8,certno9,certno10)) as certificate from table where trtm='TM'
  ----------
  my output is:
   ---------
   total_id      certificate
       3             3
  -------------------------
我只得到total_id和certificate中的计数。因此,如果有人知道,请帮助我。

假设“空”certX字段为空,您需要以下内容:

  ------------ 
  output needs:
  -----------
  total_id      certificate
       3             12
  -------------
每个
都不是null
测试将返回一个布尔值true/false,mysql将其类型转换为整数,并将其转换为基本的
1+1+0+1+..
-类型加法操作。

假设“空”certX字段为null,您需要这样的操作:

  ------------ 
  output needs:
  -----------
  total_id      certificate
       3             12
  -------------

每个
都不为空
测试将返回一个布尔真/假,mysql将其类型转换为整数,并转换为基本的
1+1+0+1+..
-类型加法运算。

单独计数,然后
求和
它:

SELECT (cert1 is not null) + (cert2 is not null) + ... (cert10 is not null)

分别计数,然后求和it:

SELECT (cert1 is not null) + (cert2 is not null) + ... (cert10 is not null)

请参阅normalisation请参阅normalisation据我所知,
sum
接受一个参数..您在这里传递了多个参数。。使用
+
添加计数。谢谢Shaharyar@sukumarK.G如果答案有助于您解决问题,请接受它。我知道,
sum
接受一个参数..您在这里传递了多个参数。。使用
+
添加计数。谢谢Shaharyar@sukumarK.G如果答案有助于你解决问题,就接受它。