1

I have this simple MYSQL table1:

id
 1
 2
 3

AND this table2:

 id | question | answer
  1 |  how     | 
  2 |  are     | fine
  3 |  you     | ok

And this simple query:

SELECT table1.id,COUNT(answer NOT NULL) FROM table1 LEFT JOIN table2
       ON table1.id = table2.id

I want to count non empty answer fields. I tried NOT NULL but it's not working as the answer column is NOT NULL and where is not applicable as it will lead the main query to return nothing

4
  • are you having group by clause in your query ? Commented Sep 19, 2014 at 13:05
  • No no need for group i think Commented Sep 19, 2014 at 13:07
  • 1
    Why are you selecting the id then? Commented Sep 19, 2014 at 13:07
  • without group by i think your query is not possible..!! Commented Sep 19, 2014 at 13:08

7 Answers 7

4

You can use CHAR_LENGTH(str) function to check length of value. Use below query :

SELECT COUNT(answer) FROM table WHERE CHAR_LENGTH(answer)>0;   

From Reference Doc.

EDIT :
You may try this query :

SELECT COUNT(answer) FROM table1 LEFT JOIN table2
       ON table1.id = table2.id AND CHAR_LENGTH(table2.answer)>0
Sign up to request clarification or add additional context in comments.

3 Comments

sorry this select is actually a LEFT JOIN but i'm simplifying the query for the sake of the question. So if I put a WHERE clause it will lead to no result
POST your query with LEFT JOIN.
@JavaDev, you need to get rid of the NOT NULL, COUNT(expr) counts 1 for every not null expr. NOT NULL converts the NULL answers into BOOLEANs, so NULL answers will be counted.
1

You could try this, to capture everything that is blank:

SELECT COUNT(id) FROM table
where answer IS NOT NULL or answer <>''

That should grab and count any row that has any value in "answer".

Or, if there is a join:

SELECT     table1.count(id), table2.question
FROM         table1 LEFT OUTER JOIN
                      tabel1 ON table1.id = table2.id
GROUP BY table1.id,table2.question
HAVING      (table2.answer <> '') OR (table2.answer IS NOT NULL)

10 Comments

sorry this select is actually a LEFT JOIN but i'm simplifying the query for the sake of the question. So if I put a WHERE clause it will lead to no result
Use HAVING then. That's what that's used for.
can you please explain more as I have never heard of having
Do not use HAVING, that is not what HAVING is for, HAVING is for filtering GROUPs.
Yes - I know. And I've added that to my answer to show that. His original post didn't say anything about a join, or extra columns. No need for a group. But if there is a join, presumably there will be more columns.
|
0

Just count Number of rows having NULL answers!

SELECT COUNT(1) FROM table /* COUNT(1) will go on counting rows frequncy*/
where answer IS NOT NULL

1 Comment

sorry this select is actually a LEFT JOIN but i'm simplifying the query for the sake of the question. So if I put a WHERE clause it will lead to no result
0

If the answer column is not nullable then IS NOT NULL will return all the rows. What you want is "is not empty string":

SELECT COUNT(*)
FROM table
WHERE answer <> "";

If answer is nullable then you can check for both conditions:

SELECT COUNT(*)
FROM table
WHERE answer IS NOT NULL AND answer <> "";

Comments

0
   SELECT COUNT(t2.answer) count
     FROM table1 t1 
LEFT JOIN table2 t2
       ON t2.id = t1.id
      AND t2.answer != ''

OR

   SELECT t1.id, COUNT(t2.answer) count
     FROM table1 t1 
LEFT JOIN table2 t2
       ON t2.id = t1.id
      AND t2.answer != ''
 GROUP BY t1.id

Comments

0

You can use this SUM(CHAR_LENGTH((answer))/CHAR_LENGTH((answer))) formula to count non empty answer fields.

SELECT table1.id, SUM(CHAR_LENGTH((answer))/CHAR_LENGTH((answer))) FROM table1 

LEFT JOIN table2 ON table1.id = table2.id

Comments

0

Count each of your columns:

SELECT count(`id`) + count(`personal_id`) + count(`f_name`) + ... 
FROM `detail_members` WHERE `personal_id` = '$personalid'

Comments

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.