2

How can I use count condition in sql query. Here is my query

SELECT COUNT(test='maths') FROM homework where id = 1;

1 Answer 1

4

In MySQL conditions have either 1 or 0 as outcome. So use sum() instead

SELECT sum(test='maths') FROM homework where id = 1;

count() just counts non-null values. So you could do it with count() too but like this

SELECT count(case when test='maths' then 1 else null end) 
FROM homework 
where id = 1;
Sign up to request clarification or add additional context in comments.

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.