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
-------------------
我需要从1到10
计算id_no和certno的数量 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
-------------------------
但我需要证书的数量,其中值为3但我需要的是12
------------
output needs:
-----------
total_id certificate
3 12
-------------
我只得到total_id和certificate.so中的计数。如果有人知道,请帮助我。
答案 0 :(得分:2)
假设“empty”certX字段为空,则需要这样的内容:
SELECT (cert1 is not null) + (cert2 is not null) + ... (cert10 is not null)
每个is not null
测试将返回一个布尔值true / false,它将由mysql对整数进行类型转换,并转换为基本的1+1+0+1+....
类型的加法运算。
答案 1 :(得分:1)
单独计算,然后计算SUM
:
SELECT
count(id_no) as total_id,
(count(certno1) +
count(certno2) +
count(certno3) +
count(certno4) +
count(certno5) +
count(certno6) +
count(certno7) +
count(certno8) +
count(certno9) +
count(certno10)) as certificate
FROM table
WHERE trtm='TM';