2016-08-30 3 views
0
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 
     ------------------- 

I Graf von id_no und certno aus 1 bis 10Wie man dorthin Zählung mehrerer Spalte sql

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 
    ------------------------- 

nehmen müssen, aber ich muss die Anzahl der Bescheinigung, wo die Werte sind, gibt es 3 aber ich brauche, ist 12

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

ich bin nur die Zählung in beiden total_id und certificate.so bekommen, bitte helfen Sie mir, wenn jemand weiß.

+0

Siehe Normalisierung – Strawberry

Antwort

2

Unter der Annahme, die „leer“ Cert x Felder sind NULL-Werte, Sie so etwas wie dieses brauchen würde:

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

Jeder is not null Test wird ein boolean true/false zurück, die auf ganze Zahlen von mysql typisieren wird, und in eine grundlegende 1+1+0+1+....-Typ Additionsoperation umgewandelt.

+0

Danke Marc B –

1

Count getrennt und dann SUM es:

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'; 
+1

ich weiß, 'sum' nimmt man argument..you in mehrere Argumente sind vorbei hier .. verwenden '+' um die Zählungen stattdessen hinzuzufügen. –

+0

Danke Shaharyar –

+0

@ sukumarK.G Akzeptieren Sie die Antwort, wenn es Ihnen geholfen hat, das Problem zu lösen. – Shaharyar