Professional Web Applications Themes

2 Queries in 1 statement? - MySQL

Hi there I have a bunch of survey results in a database table, about 10,000 right now. I'd like to know how many answered 'yes' and how many answered 'no' to a particular question. I could query the database twice with... SELECT Count(QuestionID) as YesCount FROM SurveyAnswers WHERE QuestionID='12' AND Answer='yes' ....and... SELECT Count(QuestionID) as NoCount FROM SurveyAnswers WHERE QuestionID='12' AND Answer='no' ....respectively, but is there a way to get both these totals in one query? I'm using MySQL 4.1.10a Thanks!...

  1. #1

    Default 2 Queries in 1 statement?

    Hi there

    I have a bunch of survey results in a database table, about 10,000 right
    now. I'd like to know how many answered 'yes' and how many answered 'no'
    to a particular question.

    I could query the database twice with...

    SELECT Count(QuestionID) as YesCount FROM SurveyAnswers WHERE
    QuestionID='12' AND Answer='yes'

    ....and...

    SELECT Count(QuestionID) as NoCount FROM SurveyAnswers WHERE
    QuestionID='12' AND Answer='no'

    ....respectively, but is there a way to get both these totals in one query?
    I'm using MySQL 4.1.10a

    Thanks!
    Good Man Guest

  2. #2

    Default Re: 2 Queries in 1 statement?

    Good Man wrote:
    > Hi there
    >
    > I have a bunch of survey results in a database table, about 10,000 right
    > now. I'd like to know how many answered 'yes' and how many answered 'no'
    > to a particular question.
    >
    > I could query the database twice with...
    >
    > SELECT Count(QuestionID) as YesCount FROM SurveyAnswers WHERE
    > QuestionID='12' AND Answer='yes'
    >
    > ...and...
    >
    > SELECT Count(QuestionID) as NoCount FROM SurveyAnswers WHERE
    > QuestionID='12' AND Answer='no'
    >
    > ...respectively, but is there a way to get both these totals in one query?
    > I'm using MySQL 4.1.10a
    >
    > Thanks!
    Pick one. This is the simplest way:
    SELECT
    Answer, Count(QuestionID)
    FROM
    SurveyAnswers
    WHERE
    QuestionID='12'
    GROUP BY
    Answer

    Or you can have it complex:

    SELECT
    COUNT( CASE WHEN Answer = 'yes' THEN QuestionID else NULL END) as yes_answer,
    COUNT( CASE WHEN Answer = 'no' THEN QuestionID else NULL END) as no_answer
    FROM
    SurveyAnswers
    WHERE
    QuestionID='12'

    ciao
    gmax

    --
    _ _ _ _
    (_|| | |(_|>< The Data Charmer
    _|
    [url]http://datacharmer.blogspot.com/[/url]
    Giuseppe Maxia Guest

  3. #3

    Default Re: 2 Queries in 1 statement?

    Giuseppe Maxia <gmax__cpan_._org> wrote in
    news:43ea5530$0$28066$4fafbaefreader1.news.tin.it :

    >> ...respectively, but is there a way to get both these totals in one
    >> query? I'm using MySQL 4.1.10a
    >>
    >> Thanks!
    >
    > Pick one. This is the simplest way:
    > SELECT
    > Answer, Count(QuestionID)
    > FROM
    > SurveyAnswers
    > WHERE
    > QuestionID='12'
    > GROUP BY
    > Answer
    thanks. i can't believe i didn't think of that :(
    Good Man Guest

Similar Threads

  1. Queries
    By free23 in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: May 18th, 03:01 PM
  2. Queries of Queries
    By slanza in forum Macromedia ColdFusion
    Replies: 1
    Last Post: June 3rd, 06:38 PM
  3. Queries Of Queries Single Quote Problem
    By TimH2O in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 1st, 07:46 PM
  4. Replies: 0
    Last Post: July 10th, 11:59 PM
  5. ASP.net Queries
    By Srinivasa Raghavan in forum ASP.NET General
    Replies: 0
    Last Post: June 27th, 04:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139