Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6548061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:54:20+00:00 2026-05-25T11:54:20+00:00

I have a table with values (field valore): CREATE TABLE values ( questionario_id INT(10)

  • 0

I have a table with values (field valore):

CREATE TABLE values (
    questionario_id INT(10) UNSIGNED NOT NULL,
    insegnamento_id INT(11) NOT NULL,
    domanda_id TINYINT(3) UNSIGNED NOT NULL,
    valore TINYINT(3) UNSIGNED NOT NULL COMMENT '1,2,3,4',
...

I want to count all the rows that have values (valore) 1 and all those having value 2, 3 and 4. The WHERE clause is common.

I could UNION:

SELECT
    COUNT(V.valore) AS valori_1
FROM
        values V
JOIN    questionari Q ON (V.questionario_id = Q.id) 
JOIN    sessioni S ON (Q.sessione_id = S.id)
WHERE   V.domanda_id = 1 AND S.anno_accademico = 0 AND S.settimana = 0
AND V.valore=1
UNION SELECT
    COUNT(V.valore) AS valori_2
FROM
        values V
JOIN    questionari Q ON (V.questionario_id = Q.id) 
JOIN    sessioni S ON (Q.sessione_id = S.id)
WHERE   V.domanda_id = 1 AND S.anno_accademico = 0 AND S.settimana = 0
AND V.valore=2
UNION
...

I don’t like this because I have to repeat the whole joins and the WHERE clauses.

For the same reason I don’t like this other form:

SELECT
    (SELECT COUNT(V.valore) FROM values V WHERE ...) AS C1,
    (SELECT COUNT(V.valore) FROM values V WHERE ...) AS C2,
    (SELECT COUNT(V.valore) FROM values V WHERE ...) AS C3,
    ...

Tried the CASE option:

 SELECT
        COUNT(CASE WHEN V.valore=1 THEN 1 ELSE 0 END) AS C1,
        COUNT(CASE WHEN V.valore=2 THEN 1 ELSE 0 END) AS C2,
        COUNT(CASE WHEN V.valore=3 THEN 1 ELSE 0 END) AS C3
    FROM
            values V
    JOIN    questionari Q ON (V.questionario_id = Q.id) 
    JOIN    sessioni S ON (Q.sessione_id = S.id)
    WHERE
        V.domanda_id = 1
    AND 
        S.anno_accademico = 0
    AND 
        S.settimana = 0

but it doesn’t work.
Any hints?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:54:21+00:00Added an answer on May 25, 2026 at 11:54 am

    Wouldn’t a group by help you?

    SELECT V.valore, COUNT(V.valore) AS valori_1
    FROM
            values V
    JOIN    questionari Q ON (V.questionario_id = Q.id) 
    JOIN    sessioni S ON (Q.sessione_id = S.id)
    WHERE   V.domanda_id = 1 AND S.anno_accademico = 0 AND S.settimana = 0
    GROUP BY V.valore
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with one field as name.The values in this fields are
I have a table in a db that has an int field, a non-unique
I have a table that has a value field. The records have values somewhat
I have a VARCHAR field in a MySQL table like so - CREATE TABLE
I'm having some trouble comparing values found in VARCHAR fields. I have a table
I have two fields in a table. One contains values such as BTA, BEA,
I have a table representing values of source file metrics across project revisions, like
I have a table with string values that need to matched individually to a
I have a table of many values where one column has the WO Number,
I have an html table containing values that are being generated from javascript. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.