L
L
Lavrov952018-04-04 18:03:09
SQL
Lavrov95, 2018-04-04 18:03:09

How to write sql correctly?

SELECT countries.short_name, COUNT(user.id), COUNT(user.id WHERE user.status = 1) FROM user INNER JOIN countries ON user.country_id = countries.country_id
GROUP BY short_name

ERROR
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE user.status = 1) FROM user INNER JOIN ON user.country_id = count' at line 1

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
awesomer, 2018-04-04
@awesomer

If you need a COUNT with a WHERE, that's not how it's done.
See HAVING

S
SagePtr, 2018-04-04
@SagePtr

Try this:
SUM(user.status = 1)

G
Game Master, 2018-04-08
@baitarakhov

SELECT 
countries.short_name, 
COUNT(user.id) as cnt_all_stat_user, 
COALESCE(sum(case when user.status = 1 then 1 end),0) as cnt_stat_1_user
FROM user 
INNER JOIN countries 
  ON user.country_id = countries.country_id
GROUP BY short_name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question