MYSQL / PHP计算了重复出现的问题的答案

时间:2015-07-08 13:34:42

标签: php mysql sql report

我有点困惑甚至不应该这样做。但我会尽力解释我能做的最好的事情。

我创建的报告看起来像这样:

enter image description here

但还有更多问题。

所以有多人回答了这项调查,所有记录都在我需要合作的数据库中来制作这份报告。

我创建了一个查询,它可以将所有相关数据从连接下面的多个表中拉回来:

表格 -调查 -surveyEntries -surveyQuestions -survey_meta -hw_services

        SELECT
            `surveyEntries`.`ID` AS EntryID,
            `surveyEntries`.`created` AS EntryDate,
            `hw_services`.`name` AS Provider,
            `surveyQuestions`.`ID` AS QuestionID,
            `surveyQuestions`.`label` AS Question,
            `survey_meta`.`answer` AS Answer,
          `surveyQuestions`.`parentID` AS ParentQuestion
        FROM `survey`
        JOIN `surveyQuestions`
            ON `survey`.`ID` = `surveyQuestions`.`surveyID`
        JOIN `surveyEntries`
            ON `survey`.`ID` = `surveyEntries`.`surveyID`
        JOIN `survey_meta`
             ON (`surveyEntries`.`ID` = `survey_meta`.`entryID` AND `surveyQuestions`.`ID` = `survey_meta`.`questionID`)
        JOIN `hw_services`
            ON `surveyEntries`.`hw_serviceID` = `hw_services`.`ID`
        WHERE `hw_services`.`healthwatchID` = '1'
        AND `survey`.`ID` = '1'
        AND `surveyQuestions`.`type` IN ('radio', 'dropdown')
        AND `hw_services`.`ID` = '1697'

好吧,不仅仅是为了证明这是我正在撤回的数据的图片。调查中共有30个问题,但在这里我只为每个条目显示4行。

enter image description here

enter image description here

所以他们是相同的问题,但不同的条目。

现在我该如何解决这个问题?

并获得表示“是”的数字和每个问题中表示“否”的数字,以便我可以在php中生成此内容?

如果您需要任何进一步的信息,请告诉我。

2 个答案:

答案 0 :(得分:1)

由于您拥有所有数据,我们可以使用group by来查看每个问题的所有唯一答案的计数。为此,我们可以使用以下内容:

SELECT QuestionId, Question, Answer, count(*) 
FROM (PUT YOUR SELECT HERE) 
GROUP BY QuestionId, Answer

然后,这将为您提供每个问题(及其ID),以及该问题的唯一答案和该唯一答案的计数。

或者在您提供的一个选项中完成所有操作:

SELECT
            `surveyEntries`.`ID` AS EntryID,
            `surveyEntries`.`created` AS EntryDate,
            `hw_services`.`name` AS Provider,
            `surveyQuestions`.`ID` AS QuestionID,
            `surveyQuestions`.`label` AS Question,
            `survey_meta`.`answer` AS Answer,
             count(*) as Total,
            `surveyQuestions`.`parentID` AS ParentQuestion
        FROM `survey`
        JOIN `surveyQuestions`
            ON `survey`.`ID` = `surveyQuestions`.`surveyID`
        JOIN `surveyEntries`
            ON `survey`.`ID` = `surveyEntries`.`surveyID`
        JOIN `survey_meta`
             ON (`surveyEntries`.`ID` = `survey_meta`.`entryID` AND `surveyQuestions`.`ID` = `survey_meta`.`questionID`)
        JOIN `hw_services`
            ON `surveyEntries`.`hw_serviceID` = `hw_services`.`ID`
        WHERE `hw_services`.`healthwatchID` = '1'
        AND `survey`.`ID` = '1'
        AND `surveyQuestions`.`type` IN ('radio', 'dropdown')
        AND `hw_services`.`ID` = '1697'
        GROUP BY `surveyQuestions`.`ID`, `survey_meta`.`answer`

我已将count(*)添加到初始选择中,并在最后添加GROUP BY surveyQuestions.ID, survey_meta.answer

答案 1 :(得分:0)

对于GENDER报告,您可以使用此sql

SELECT
        `survey_meta`.`answer` AS Gender,
        COUNT(*) AS 'Total Answer'
    FROM `survey`
    JOIN `surveyQuestions`
        ON `survey`.`ID` = `surveyQuestions`.`surveyID`
    JOIN `surveyEntries`
        ON `survey`.`ID` = `surveyEntries`.`surveyID`
    JOIN `survey_meta`
         ON (`surveyEntries`.`ID` = `survey_meta`.`entryID` AND `surveyQuestions`.`ID` = `survey_meta`.`questionID`)
    JOIN `hw_services`
        ON `surveyEntries`.`hw_serviceID` = `hw_services`.`ID`
    WHERE `hw_services`.`healthwatchID` = '1'
    AND `survey`.`ID` = '1'
    AND `surveyQuestions`.`type` IN ('radio', 'dropdown')
    AND `hw_services`.`ID` = '1697'
    AND `surveyQuestions`.`ID` = 9
GROUP BY `survey_meta`.`answer`

对于AGE GROUP报告,与上述类似,只需更改surveyQuestionsID。所以代码就像这样

SELECT
        `survey_meta`.`answer` AS Gender,
        COUNT(*) AS 'Total Answer'
    FROM `survey`
    JOIN `surveyQuestions`
        ON `survey`.`ID` = `surveyQuestions`.`surveyID`
    JOIN `surveyEntries`
        ON `survey`.`ID` = `surveyEntries`.`surveyID`
    JOIN `survey_meta`
         ON (`surveyEntries`.`ID` = `survey_meta`.`entryID` AND `surveyQuestions`.`ID` = `survey_meta`.`questionID`)
    JOIN `hw_services`
        ON `surveyEntries`.`hw_serviceID` = `hw_services`.`ID`
    WHERE `hw_services`.`healthwatchID` = '1'
    AND `survey`.`ID` = '1'
    AND `surveyQuestions`.`type` IN ('radio', 'dropdown')
    AND `hw_services`.`ID` = '1697'
    AND `surveyQuestions`.`ID` = 10
GROUP BY `survey_meta`.`answer`