E
E
eternalfire2019-12-02 17:19:27
SQL
eternalfire, 2019-12-02 17:19:27

How to write a query that checks multiple conditions for different rows?

Hey!
Help me solve one problem, I can't figure out how to implement
it. I have such a structure from the
services
| tables. id | category |
-----------------
| 1 | 4 |
-----------------
| 2 | 5 |
users
| id | name |
-----------------
| 1 | test |
-----------------
| 2 | test |
user_services
| id | service_id | user_id | end |
------------------------------
| 1 | 1 | 1 | 0 |
------------------------------
| 2 | 2 | 2 | 1 |
How do I get all users who have user_services entries with service.category not 3 and not 5 or 3 or 5 but end = 1 and who have user_services entries which service.category is not 3 and not 5 and end = 0
I can’t figure it out, thank you for your attention and understanding and all the best!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman, 2019-12-02
@eternalfire

SELECT * FROM users u
WHERE EXISTS(SELECT 'x' FROM `user_services` us JOIN `services` s ON s.`id` = us.`service_id`  WHERE us.`user_id` = u.`id` AND s.`category` <не 3 и не 5 или 3 или 5, но end = 1>)
AND (< которых есть записи user_services у которых service.category не 3 и не 5 и end = 0
>)

you can also count the number of rows you are looking for by grouping the result of joining three tables by users.id and cut off those that do not fit into having

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question