Arpi
Arpi

Reputation: 25

Sort users by their meta data's value

Have a table like this:

users tbl:

id | email
1  | [email protected]
2  | [email protected]

And profile table:

profiles tbl:

user_id | name | value
1       | name | John
1       | age  | 24
2       | name | Ana

Is there a way to sort the users by their profile name? I want the end result to be something like this:

2 | [email protected] | Ana
1 | [email protected] | John

Sorted by their name.

Upvotes: 1

Views: 34

Answers (1)

cdhowie
cdhowie

Reputation: 169318

Yes, just join the tables as usual and order by profiles.value.

SELECT users.id,
       users.email,
       profiles.value AS user_name,
INNER JOIN profiles
    ON users.id = profiles.user_id AND
       profiles.name = "name"
ORDER BY profiles.value;

Upvotes: 1

Related Questions