There are 2 tables: users and props. You can see here . I do a sample of ID
from users
whose property = 31
and value = 'Y'
This query will display 1, 5 :
SELECT DISTINCT U.id AS ID FROM users U INNER JOIN props UP ON U.id = UP.id_user WHERE (U.country = 20) AND (UP.property=31 AND UP.value='Y')
You need to do a reverse request, i.e. those records that do not have this UP.property=31
. T e should output 3, 6 instead of all (except country = 30
)
Schema and data scripts:
CREATE TABLE users (`id` int, `name` varchar(15), `country` int) ; CREATE TABLE props (`id` int, `property` int, `id_user` int, `value` varchar(2)) ; INSERT INTO users (`id`, `name`, `country`) VALUES (1, 'vadim', 20), (2, 'petya', 20), (3, 'vasya', 20), (4, 'serega', 30), (5, 'lesha', 20), (6, 'vitya', 20) ; INSERT INTO props (`id`, `property`, `id_user`, `value`) VALUES (1, 31, 1, 'Y'), (2, 33, 1, 'Y'), (3, 36, 1, 'Y'), (4, 31, 2, 'N'), (5, 33, 2, 'Y'), (6, 35, 3, 'N'), (7, 32, 3, 'Y'), (8, 30, 4, 'Y'), (9, 32, 4, 'Y'), (10, 31, 5, 'Y'), (11, 34, 5, 'Y'), (12, 36, 6, 'Y'), (13, 35, 6, 'Y') ;