按用户数据的值对用户进行排序

时间:2014-08-21 05:45:14

标签: php mysql

有这样一张桌子:

用户tbl:

id | email
1  | email@example.com
2  | abc@example.com

和个人资料表:

个人资料tbl:

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

有没有办法根据个人资料名称对用户进行排序?我希望最终结果是这样的:

2 | abc@example.com | Ana
1 | email@example.com | John

按名称排序。

1 个答案:

答案 0 :(得分:1)

是的,只需像往常一样加入表格,然后按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;
相关问题