Mysql连接顺序

时间:2011-12-02 07:35:44

标签: mysql sql

我有两张表如下:

表user_prefs:

user_prefs_id    int(11) PK
user_id          int(11)
item             varchar(128) PK
category_id      smallint(6)
parent           smallint(6)
rank             tinyint(4)
dateadded        datetime

表类别:

category_id      int(11) PK
name             varchar(255)
main_category    varchar(64)
parent           int(6)
isparent         enum('No','Yes')
display_rank     int(9)
active           enum('Yes','No')

我正在根据user_prefsuser_id进行选择,但我想按类别表中的类别名称排序 - category_id在两个表之间匹配

这可能吗?

4 个答案:

答案 0 :(得分:5)

SELECT u.*
FROM user_prefs u
   JOIN categories cat ON u.category_id = cat.category_id
WHERE p.user_id = 10 /* Put user id here*/
ORDER BY cat.name

答案 1 :(得分:0)

select u.*, c.category
from user_prefs u
INNER JOIN categories c ON u.category_id = c.category_id
order by c.name

答案 2 :(得分:0)

...不确定

SELECT p.*
FROM user_prefs p
JOIN categories c ON (p.cateogory_id = c.category_id)
WHERE p.user_id = ?
ORDER BY c.name;

答案 3 :(得分:0)

这应该对你有用

SELECT up.*, c.name
FROM user_prefs up
JOIN categories c ON c.category_id = up.category_id
WHERE up.user_id = ?
ORDER BY c.name;