Answer the question
In order to leave comments, you need to log in
How to make a selection from the MySql database by condition - only if all records have such a value?
There is a table "order":
id | status_id | user_id
1 | 0 | 1
2 | 1 | 1
3 | 0 | 2
4 | 0 | 2
5 | 0 | 3
6 | 1 | 4
It is necessary to display all user_id who have ALL orders with status 0, that is, the result should be:
2 and 3
Or only with status 1, then the result should be:
4
Answer the question
In order to leave comments, you need to log in
Simple, but most likely not the best option.
SELECT * FROM table GROUP BY user_id HAVING SUM (status_id) = 0
gospidi:
orselect user_id from order where status_id = '1';
SELECT user_id FROM table
GROUP BY user_id
HAVING SUM (ABS(status_id)) -COUNT(status_id)=0
or
HAVING SUM (ABS(status_id)) = COUNT(status_id) will output
2,3,4
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question