我有一个代码,无法计算如何计算值的出现次数,例如" c"的总和。 db中的数字是齿数,x表示缺牙,c表示龋齿,w表示填充。
SELECT SUM(18='w'+17='w'+16='w'+15='w'+14='w'+13='w'+12='w'+11='w'+21='w'+22='w'+23='w'+24='w'+25='w'+26='w'+27='w'+28='w'+48='w'+47='w'+46='w'+45='w'+44='w'+44='w'+43='w'+42='w'+41='w'+31='w'+32='w'+33='w'+34='w'+35='w'+36='w'+37='w'+38='w') AS liczbaw FROM badania
表格如下:
答案 0 :(得分:0)
首先要注意的是,您可能希望避免将列名称作为数字...根据" Can a number be used to name a MySQL table column?",我必须引用后退(后退)引号)。
我实际上不确定您是要在多行上执行此操作,而是要聚合多行 - 您可以按以下方式执行操作
SELECT SUM(CASE WHEN `18` = 'c' THEN 1 ELSE 0 END) AS cnt_18_c FROM badania
所以,我在我的sql中使用CASE语句 - 您可以在" Case statement in MySQL"上找到更多信息。您也可以使用SUM(CASE...END)
is synonymous to a SUM(IF(...))
语句进行不同的操作。
如果您想要多个列和行,那么您只需添加您感兴趣的各个总和,如下所示:
SELECT
SUM(CASE WHEN `18` = 'c' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN `17` = 'c' THEN 1 ELSE 0 END)
AS cnt_18_17_c FROM badania
可以使用GROUP BY
定义不同的分组(除了所有分组)。如果它是逐行的 - 那么甚至不需要SUM。
SELECT
CASE WHEN `18` = 'c' THEN 1 ELSE 0 END
+ CASE WHEN `17` = 'c' THEN 1 ELSE 0 END
AS cnt_18_17_c FROM badania
在后一种情况下,您还可以使用更智能的技术。您可以concatenate将所有牙齿作为字符串,并计算字符串中的出现次数,假设使用mysql的标准API集为您提供了这样的字符数。 - 或者您可以从编程环境(PHP)中完成计数。您还可以使用其他smart way to do those character counts。
所以我举了几个例子 - 希望我的MySQL语法正确。没有引用你确切的问题 - 我怀疑我可能已经覆盖了它。
答案 1 :(得分:-1)
该查询假设您想要查看特定患者,并且正在猜测如何完成此操作 - 相应地进行调整。如果要聚合所有患者,请删除where子句。
select tooth_code, count(*)
from ( select 18 as tooth_code from badania where idb=2 and pacjent=25 union all
select 17 from badania union where idb=2 and pacjent=25 all
select 16 from badania union where idb=2 and pacjent=25 all
select 15 from badania union where idb=2 and pacjent=25 all
select 14 from badania union where idb=2 and pacjent=25 all
select 13 from badania union where idb=2 and pacjent=25 all
select 12 from badania union where idb=2 and pacjent=25 all
select 11 from badania union where idb=2 and pacjent=25 all
select 21 from badania union where idb=2 and pacjent=25 all
select 22 from badania union where idb=2 and pacjent=25 all
select 23 from badania union where idb=2 and pacjent=25 all
select 24 from badania union where idb=2 and pacjent=25 all
select 25 from badania union where idb=2 and pacjent=25 all
select 26 from badania union where idb=2 and pacjent=25 all
select 27 from badania union where idb=2 and pacjent=25 all
select 28 from badania union where idb=2 and pacjent=25 all
select 47 from badania union where idb=2 and pacjent=25 all
select 46 from badania union where idb=2 and pacjent=25 all
select 45 from badania union where idb=2 and pacjent=25 all
select 44 from badania union where idb=2 and pacjent=25 all
select 43 from badania union where idb=2 and pacjent=25 all
select 42 from badania union where idb=2 and pacjent=25 all
select 41 from badania union where idb=2 and pacjent=25 all
select 31 from badania union where idb=2 and pacjent=25 all
select 32 from badania union where idb=2 and pacjent=25 all
select 33 from badania union where idb=2 and pacjent=25 all
select 34 from badania where idb=2 and pacjent=25 union all
select 35 from badania where idb=2 and pacjent=25 union all
select 36 from badania where idb=2 and pacjent=25 union all
select 37 from badania where idb=2 and pacjent=25 union all
select 38 from badania where idb=2 and pacjent=25
)
group by tooth_code