0

Is it possible to count the number of populated fields that have a given prefix?

e.g. I have a table of Q&As with multiple choice answers. Sometimes a question has 3 answer options, sometimes there a 4 or 5. Each answer is in a field such as answer1_a, answer1_b, answer1_c, etc, -- is there a way to identify how many of these fields have data (for a given record)? I need this to determine how many multiple choice options to present in the HTML.

Perhaps using mysql_num_fields in some way?

thanks, Geoff

2
  • use looping. either way you need to get the value of this fields, right? do your checking on the loop and dont display it if null or empty. Commented Jun 15, 2012 at 6:12
  • Can you post your table structure? Commented Jun 15, 2012 at 6:18

2 Answers 2

2

Given your table structure, Kaii's answer is correct.

For the future I would suggest using a real relational model like this:

CREATE TABLE QUESTION(
  ID INTEGER NOT NULL,
  QUESTION VARCHAR(100) NOT NULL,

  PRIMARY KEY (ID)
);

INSERT INTO QUESTION VALUES(1, 'My first question');
INSERT INTO QUESTION VALUES(2, 'My second question');
INSERT INTO QUESTION VALUES(3, 'My third question');

CREATE TABLE ANSWER(
  ID INTEGER NOT NULL,
  ID_QUESTION INTEGER NOT NULL,
  ANSWER VARCHAR(100) NOT NULL,

  PRIMARY KEY (ID),
  FOREIGN KEY (ID_QUESTION) REFERENCES QUESTION(ID)
);

INSERT INTO ANSWER VALUES (1, 1, 'First possible answer for question 1');
INSERT INTO ANSWER VALUES (2, 1, 'Second possible answer for question 1');
INSERT INTO ANSWER VALUES (3, 1, 'Third possible answer for question 1');
INSERT INTO ANSWER VALUES (4, 2, 'First possible answer for question 2');
INSERT INTO ANSWER VALUES (5, 2, 'Second possible answer for question 2');
INSERT INTO ANSWER VALUES (6, 3, 'First possible answer for question 3');
INSERT INTO ANSWER VALUES (7, 3, 'Second possible answer for question 3');
INSERT INTO ANSWER VALUES (8, 3, 'Third possible answer for question 3');
INSERT INTO ANSWER VALUES (9, 3, 'Fourth possible answer for question 3');

SELECT
  QUESTION.ID,
  COUNT(*) as NB_ANSWER
FROM QUESTION
INNER JOIN ANSWER ON QUESTION.ID = ANSWER.ID_QUESTION
GROUP BY QUESTION.ID
ORDER BY QUESTION.ID;

See it work here: http://sqlfiddle.com/#!3/0025a/3/0

Sign up to request clarification or add additional context in comments.

1 Comment

+1 - May I also suggest ALTER TABLE answer ADD COLUMN sequence INT NOT NULL; ALTER TABLE answer ADD UNIQUE (id_question, sequence); (not syntactically correct but you get the point).
1

To solve this in SQL you would have to list all field names explicitly .. Assuming your field values are set to NULL when no data is given, an SQL solution may look like this:

SELECT *, 
    ( answer1_a IS NOT NULL +
      answer1_b IS NOT NULL +
      answer1_c IS NOT NULL +
      answer1_d IS NOT NULL +
      answer1_e IS NOT NULL ) AS number_of_answers
FROM answers

A scalable approach (that grows automatically when you change the table structure e.g. to insert a new answer 'f') would be to do this kind of calculation in PHP:

<?php
$res = mysql_query("SELECT * FROM answers");
while ( $row = mysql_fetch_assoc($res) ) {
    $count = 0;
    foreach ( $row as $fieldname => $value ) {
        if ( strncmp($fieldname, 'answer1_', 8) === 0 &&
             $value !== NULL ) {
            $count++;
        }
    }
    $row['number_of_answers'] = $count;
    $resultset[] = $row;
}
print_r($resultset);

However, the PHP solution has the drawback that you can not easily use a HAVING or WHERE clause in your SELECT statement to find (for example) all questions that have 4 possible answers.

1 Comment

I don't think you would need the SUM(), just them added together should be fine if each row is a unique question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.