Skip to content
Advertisement

mysql: Select all rows and compare with other table

I have two tables and need to get all rows from the first one and then check which values from the second table match the first table.

My goal is to get all so called ‘achievements’ and then check which one the user has reached.

achievements

achievement_user

My desired result for a query where I check the results for userID = 1 would be something like

The new column solvedByUser could be basically any datatype (boolean, int, …). I just need a list of all available achievements and then see which one the user has reached.

Advertisement

Answer

You can left join the achievments table with achievement_user:

solvedByUser is a 0/1 flag that indicates whether the given achievement was reached by the given user.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement