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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With