Mysql joins

I have two tables:

User_prefs table:

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 make a selection from user_prefs based on user_id , but I want to sort by category name, which is in the category table - category_id mapped between two tables

Is it possible?

+4
source share
4 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 
+5
source
 select u.*, c.category from user_prefs u INNER JOIN categories c ON u.category_id = c.category_id order by c.name 
0
source

Sure...

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

This should work for you.

 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; 
0
source

Source: https://habr.com/ru/post/1384264/


All Articles