I have a table named users.
id name date cdate c a b
1 rz 42121221 42121221 0 1 0
2 an 12122111 42121221 0 0 1
3 cb 22121221 42121221 1 1 1
4 ss 3321221 42121221 1 0 0
I have to select the values between two dates, and then i have to find the count using this statement.
SELECT COALESCE(SUM(IF(c=1 AND a=0 AND b=1 , 1, 0)),0) AS ACTIVE
WHERE DATE BETWEEN 'DATE 1' AND 'DATE 2',
COALESCE(SUM(IF(c=0 AND a=0 AND b=1 , 1, 0)),0) AS INC
WHERE cdate BETWEEN 'DATE 1' AND 'DATE 2'
FROM users where dealer id='2'
This query is not working.
Here’s your query, properly formatted. You can only have one WHERE clause per SELECT statement, and the FROM clause goes after the SELECT clause and before the WHERE clause.
I copied your date comparisons into the IF statements.
Also, I removed the COALESCE clause because it was redundant in this case. SUM always returns a value.
I put the columns together, separated by a comma, after the SELECT clause.
You may wish to remove the WHERE clause now, depending on what you’re trying to do.
I’m also suspicious of
cdate BETWEEN 'DATE 1' AND 'DATE 2'. If cdate is a DATETIME column, you’ll want to put in properly formatted dates in the constants. If DATE 1 and DATE 2 are column names, then you’ll need back ticks, not single quotes, like this:You should consider reading the documentation on the SELECT syntax.