Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql join order by

Tags:

sql

mysql

I have two tables as follows:

Table 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

Table categories:

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')

I am doing a select from user_prefs based on user_id, but I want to sort by category name which is in the categories table - category_id is matched between the two tables

Is this possible?

like image 226
StudioTime Avatar asked Dec 02 '11 07:12

StudioTime


1 Answers

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
like image 52
Elias Hossain Avatar answered Nov 11 '22 04:11

Elias Hossain