微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

查询以从每个问题的答案集中选择已回答所有问题的用户

如何解决查询以从每个问题的答案集中选择已回答所有问题的用户

|| 这是MysqL表。
            User    Question    Answer
            20      1           answer1_2
            20      2           answer2_5
            20      3           answer3_1
            726     1           answer1_2
            726     2           answer2_5
            726     3           answer3_1
我希望已经回答所有问题的用户 我们为每个问题预定义了一组答案。 像问题1一样可以从answer1_1,answer1_2,answer1_3得到答案      问题2可以包含answer2_1,answer2_2,answer2_3的答案 最初,我尝试过这样的查询
            SELECT GROUP_CONCAT( disTINCT CAST( User AS CHAR ) ) allUsers,COUNT( disTINCT User ) totalUsers
            FROM survey
            WHERE
            (
                (
                Question =1
                AND Answer =  \'answer1_2\'
                )
                OR (
                Question =1
                AND Answer =  \'answer1_2\'
                )
                OR (
                Question =1
                AND Answer =  \'answer1_3\'
                )
            )
            AND (
                (
                Question =2
                AND Answer =  \'answer2_1\'
                )
                OR (
                Question =2
                AND Answer =  \'answer2_2\'
                )
                OR (
                Question =2
                AND Answer =  \'answer2_3\'
                )
            )
但随后我看到了结果,并意识到所有问题中的“与”都无法满足我的要求 但我需要那种逻辑的结果。 表结构无法更改。     

解决方法

您可以执行以下操作:
Select user,count(answer) from survey group by user having count(answer)=3
    ,如果您在字段
User
Question
上具有唯一键,则可以执行以下操作:
SELECT count(Question) answers_no,.... FROM survey GROUP BY User HAVING answers_no = num_total_questions
您可以将num_total_questions作为变量从应用程序传递     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。