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

发布于 2024-11-09 12:15:27 字数 1447 浏览 0 评论 0原文

这是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'
                )
            )

但后来我看到结果并意识到所有问题中的“AND”不能满足我的要求 但我需要这个逻辑的结果。

表结构无法更改。

This is mysql table.

            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

I want the users who has given answer of all questions and
we have predefined set of answers for each question.

like Question 1 can have answers from answer1_1, answer1_2, answer1_3
Question 2 can have answers from answer2_1, answer2_2, answer2_3

Initially, I tried query like this:

            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'
                )
            )

but then I see the result and realize that 'AND' among all questions won't work for my requirement
but I need the result with that logic.

Table structure cannot be changed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浸婚纱 2024-11-16 12:15:27

你可以这样做:

Select user, count(answer) from survey group by user having count(answer)=3

You could do something like:

Select user, count(answer) from survey group by user having count(answer)=3
慵挽 2024-11-16 12:15:27

如果您在字段 UserQuestion 上有唯一的键,那么您可以执行以下操作:

SELECT count(Question) answers_no, .... FROM survey GROUP BY User HAVING answers_no = num_total_questions

您可以将 num_total_questions 作为应用程序中的变量传递

If you have a unique key on fields User and Question, then you could do something like this:

SELECT count(Question) answers_no, .... FROM survey GROUP BY User HAVING answers_no = num_total_questions

You could pass num_total_questions as a variable from application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文