1

I have next mysql table:

id | object_id | status

where object_id is not unique and status is tinyint(-1 or 1).

That is, for one object I can have several identical and / or different statuses.

I want to make SELECT query with getting object_id, COUNT of positive status (as pos) and COUNT of negative status(as neg) WHERE neg>pos;

I tried this, but i have problem with select several(vote_up and vote_down) columns from one(vote) using condition

SELECT * FROM (
    SELECT object_id, COUNT(status) as (IF(status>0,'status_up','status_down'))
    FROM object_statuses GROUP BY object_id;
) WHERE status_up<status_down;
4

1 Answer 1

4

By viewing your query i guess you are trying to count the negative votes and positive votes from users and return users who have greater negative votes count. If that is the case you can use below query

SELECT object_id,
sum(status  = 1) status_up,
sum(status  = -1) status_down
FROM object_statuses
GROUP BY object_id
HAVING status_up < status_down

You cannot use WHERE clause to perform filter on aggregate results instead use HAVING clause for such operations

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

3 Comments

Well, you can in the context provided by the OP.
it's good and right answer. It's my fault - I'm using two different schemas(at table and query) in my question
@Strawberry yup in parent query this can be performed but not at same level query

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.