Utenti che non hanno un badge particolare

Sto cercando di costruire una query che mostri gli utenti di un gruppo che non hanno un determinato badge, come “Certified”.

Qualcosa del tipo: “Dammi tutti gli utenti del gruppo VIP che non hanno il badge Certified”. Grazie.

Utenti che non possiedono un badge specifico

Proviamo così:

WITH exclude_badge AS (
SELECT gu.user_id
FROM badges b, user_badges ub, users u, group_users gu
WHERE u.id = ub.user_id
AND ub.badge_id = b.id
AND u.id = gu.user_id
AND b.name = 'Certified'
AND gu.id = 10 
)

SELECT
u.id AS user_id
FROM users u
WHERE u.id NOT IN (SELECT * FROM exclude_badge)
ORDER BY user_id
LIMIT 10

Grazie per il tuo aiuto, Sid. Ho apportato alcune modifiche e ho fatto funzionare tutto con quanto segue:

SELECT gu.user_id
FROM badges b, user_badges ub, users u, group_users gu
WHERE u.id = ub.user_id
AND ub.badge_id = b.id
AND u.id = gu.user_id
AND b.name = 'Certified'
AND gu.group_id = 42
)

SELECT
u.id AS user_id
FROM users u, group_users gu
WHERE u.id = gu.user_id
AND gu.group_id = 42
AND u.id NOT IN (SELECT * FROM exclude_badge)
ORDER BY user_id
LIMIT 100

Membri del gruppo che non possiedono un particolare badge

Ottimo! Aggiungiamo i parametri e aggiorniamo la mia lista di query :wink:

Versione finale: