如何从单行计算字段值?

时间:2013-04-18 14:27:48

标签: php mysql mysqli

我是PHP和MySQL的新手,我正在努力解决这个问题...我正在从PHP插入页面收集表单数据。这是一份调查问卷。有20个问题,所有答案都是ABC。该表有一个id列,每个问题都有一列(Q1, Q2, Q3... Q20)。数据可能看起来像这样;

+------+-----+
|  id  |  1  |
+------+-----+
|  Q1  |  A  |
|  Q2  |  B  |
|  Q3  |  A  |
|  .   |  .  |
|  .   |  .  |
|  .   |  .  |
|  Q20 |  C  |
+------+-----+

现在,我要做的是计算ABC在一行中出现的值(例如id=1

我已经找到了很多方法来计算多列中的值,但到目前为止还没有找到一种方法来计算/分组单行中的值。

3 个答案:

答案 0 :(得分:0)

在PHP中,您可以将查询结果加载到数组中,然后使用array_count_values获取每个答案的计数: Working Fiddle

答案 1 :(得分:0)

免责声明:从内存中可能无法正常工作。

这个怎么样?

SELECT 1 AS 1, (SELECT COUNT(1)FROM Questions WHERE 1 ='A')AS A, (SELECT COUNT(1)FROM Questions WHERE 1 ='B')AS B, (SELECT COUNT(1)FROM Questions WHERE 1 ='C')AS C

希望这会有所帮助。

答案 2 :(得分:0)

以下是使用MySQL查询的方法:

select id,
  sum(case when `1` = 'A' then 1 else 0 end) as CountA,
  sum(case when `1` = 'B' then 1 else 0 end) as CountB,
  sum(case when `1` = 'C' then 1 else 0 end) as CountC
from SurveyTable
group by id
order by id;

这是一个SQL Fiddle,测试数据有限。


<强>附录即可。卡洛斯发布了一个更新的结构,导致以下答案。希望这些很接近:)

这将为您提供一个非常宽的行总计:

select
  sum(case when Q1 = 'A' then 1 else 0 end) as Q1CountA,
  sum(case when Q1 = 'B' then 1 else 0 end) as Q1CountB,
  sum(case when Q1 = 'C' then 1 else 0 end) as Q1CountC,
  sum(case when Q2 = 'A' then 1 else 0 end) as Q2CountA,
  sum(case when Q2 = 'B' then 1 else 0 end) as Q2CountB,
  sum(case when Q2 = 'C' then 1 else 0 end) as Q2CountC,
  sum(case when Q3 = 'A' then 1 else 0 end) as Q3CountA,
  sum(case when Q3 = 'B' then 1 else 0 end) as Q3CountB,
  sum(case when Q3 = 'C' then 1 else 0 end) as Q3CountC,
  sum(case when Q4 = 'A' then 1 else 0 end) as Q4CountA,
  sum(case when Q4 = 'B' then 1 else 0 end) as Q4CountB,
  sum(case when Q4 = 'C' then 1 else 0 end) as Q4CountC,
  sum(case when Q5 = 'A' then 1 else 0 end) as Q5CountA,
  sum(case when Q5 = 'B' then 1 else 0 end) as Q5CountB,
  sum(case when Q5 = 'C' then 1 else 0 end) as Q5CountC
from SurveyTable;

如果你想为每个问题获得一行,那么试试这个:

select
  QuestionID,
  sum(case when Answer = 'A' then 1 else 0 end) as CountA,
  sum(case when Answer = 'B' then 1 else 0 end) as CountB,
  sum(case when Answer = 'C' then 1 else 0 end) as CountC
from (
    select 'Question1' as QuestionID, Q1 as Answer from surveytable
    union all select 'Question2', Q2 from surveytable
    union all select 'Question3', Q3 from surveytable
    union all select 'Question4', Q4 from surveytable
    union all select 'Question5', Q5 from surveytable) x
group by QuestionID

有一个小提琴here


另一个附录ID所需的计数,因为每行只有一个ID,所以不需要SUM

这改变了方法。它首先将答案串在一起:

concat(q1,q2,q3,q4,q5) -- result for ID=1 in the test data: 'ABCAC'

...然后它会从字符串中删除A的每一次:

replace(concat(q1,q2,q3,q4,q5), 'A', '') -- result for ID=1: 'BCC'

...第一个字符串(ABCAC)的长度为5,第二个字符串(BCC)的长度为3.长度的差异是{{1答案:A。这和我能解释的一样。现在查询:

2

更新的小提琴是here

这仅提供原始数据。格式化将有点棘手,但它不应该太糟糕,特别是如果你使用前端语言。如果必须使用MySQL,将上述内容放入子查询并在外部查询中应用格式可能会更容易:

select
  id,
  5 - length(replace(concat(q1,q2,q3,q4,q5), 'A', '')) AS CountA,
  5 - length(replace(concat(q1,q2,q3,q4,q5), 'B', '')) AS CountB,
  5 - length(replace(concat(q1,q2,q3,q4,q5), 'C', '')) AS CountC
from surveytable;