I searching for many times now on a complex MySQL query.
It's an achievement system.
Structure of tables is simple and form is symmetric.
The first table (achievements_base) contains the achievements reference : each achievement have : - an ID (id of the row, auto-increment & unique), - a code_id (reference of the achievement), - a rank_id (sub-category of achievement's code_id), - and the achievement name (display purposes).
The second table (achievements_user) contains the achievements obtained by the users : each achievement unlocked have : - an ID (if of the row, auto-increment & unique), - the code_id (reference of the achievement), - a rank_id (sub-category of achievement's code_id), - and the user id.
The tables syntax is :
achievements_base
id - code_id - rank_id - name
1 - 1 - 1 - foo
2 - 1 - 2 - bar
3 - 1 - 3 - foobar
4 - 1 - 4 - foofoo
5 - 2 - 1 - barbar
achievements_user
id - code_id - rank_id - user_id
1 - 1 - 1 - 1
2 - 1 - 2 - 1
3 - 2 - 1 - 1
I want to display achievements that user don't have (GROUP BY code_id)
In the table exemple above, for example, expected result is, for the user_id 1 :
3 - 1 - 3 - foobar
But I don't see how to do that ! I've already tried multiples queries.
Sorry for my English, I'm French !
Thanks !
edit, example of one of queries that I've tried :
SELECT AB.name
FROM achievements_base AB
RIGHT JOIN achievements_users AU
ON AU.code_id = AB.code_id AND AU.rank_id = AB.rank_id
WHERE (SELECT COUNT(*) FROM achievements_users AU WHERE AU.user_id = 1) = 0
GROUP BY AB.code_id
ORDER BY AB.code_id ASC