php - Count SQL Syntax COUNT (value) multiple columns -
hello im trying learn count in sql, cant count how many times value in specific column
my database structure this. (table : natural)
id | 1 | 2 | 3 | 1 | 34 | 45 | 80 | 2 | 41 | 34 | 7 | 3 | 7 | 18 | 22 | 4 | 8 | 7 | 45 |
im trying this
$result=mysql_query("select count(one) total natural one=7")or die("error: ".mysql_error()); $data=mysql_fetch_assoc($result); echo $data['total'];
but 1 column in count cant result..
what need count how many times "value" (for example 7) in columns
like in example value 7 = 3 total (multiple columns)
how can make sql that.
edit: trying (where syntax problem?)
$result=mysql_query("select count(distinct id) totalcount tblname 7 in (one, two, three)")or die("error: ".mysql_error()); $data=mysql_fetch_assoc($result); echo $data['totalcount'];
i suck, answers, think problems mysql_query () cause got syntax problem, answers , me.
$result=mysql_query("select (sum(case when 1 = 7 1 else 0 end) + sum(case when 2 = 7 1 else 0 end) + sum(case when 3 = 7 1 else 0 end)) totalcount natural")or die("error: ".mysql_error()); $data=mysql_fetch_assoc($result); echo $data['totalcount'];
to fix last code, use one
, two
... on, , natural
thats correct syntax :d
this 1 give right answer if values repeat across columns
select (sum(case when 1 = 7 1 else 0 end) + sum(case when 2 = 7 1 else 0 end) + sum(case when 3 = 7 1 else 0 end)) totalcount table1
if have following data
| id | 1 | 2 | 3 | -------------------------- | 1 | 34 | 45 | 80 | | 2 | 41 | 7 | 7 | | 3 | 7 | 18 | 22 | | 4 | 7 | 7 | 45 |
output be
| totalcount | -------------- | 5 |
Comments
Post a Comment