Skip to content
Advertisement

Filter data by multiple rows in second table MYSQL

I have 2 tables. users and users_meta. I want the query to return all users that have sexe=M and user_att1=1 (which should return only mathew) but I also need to be able to filter by other attributes as well.

users table is organized as such:

users_meta table is organized as such:

I tried using group_concat and concat and could not get the desired result.

This is my current query, which gives the desired output but I am not filtering by the attribute if that makes sense.

Thanks for any help or pointing me in the right direction

Advertisement

Answer

Given your use case, I would use conditional aggregation:

The nice thing about this technique is that you can add more criteria on users_meta.meta_key simply by adding more conditions in the HAVING clause.

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