How MySQL is sorted by specific ID
How MySQL is sorted by specific ID
SET foreign_key_checks=0;
-- ----------------------------
--Table structure for ' P '
-- ----------------------------
DROP TABLE IF EXISTS ' P ';
CREATE TABLE ' P ' (
' id ' int (one) not NULL auto_increment,
' Name ' varchar (255) Default NULL,
' categories_id ' int (one) default NULL,
PRIMARY KEY (' id ')
) Engine=myisam auto_increment=7 DEFAULT Charset=utf8;
-- ----------------------------
--Records of P
-- ----------------------------
INSERT into ' P ' VALUES (' 1 ', ' Jimmy ', ' 2 ');
INSERT into ' P ' VALUES (' 2 ', ' Tina ', ' 2 ');
INSERT into ' P ' VALUES (' 3 ', ' dd ', ' 2 ');
INSERT into ' P ' VALUES (' 4 ', ' Hello ', ' 2 ');
INSERT into ' P ' VALUES (' 5 ', ' World ', ' 2 ');
INSERT into ' P ' VALUES (' 6 ', ' Slucky ', ' 2 ');
SET foreign_key_checks=0;
-- ----------------------------
--Table structure for ' p_sort '
-- ----------------------------
DROP TABLE IF EXISTS ' p_sort ';
CREATE TABLE ' P_sort ' (
' categories_id ' int (ten) not NULL default ' 0 ',
' best_sort_person_id ' varchar default NULL,
PRIMARY KEY (' categories_id ')
) Engine=myisam DEFAULT Charset=utf8;
-- ----------------------------
--Records of P_sort
-- ----------------------------
INSERT into ' p_sort ' VALUES (' 2 ', ' 2,5,1 ');
------Solution--------------------
SELECT * from P, p_sort
Find_in_set (P.id, p_sort.best_sort_person_id) >0 desc is used to row id=2,5,1 in front
Find_in_set (P.id, p_sort.best_sort_person_id) ASC is used to arrange the id=2,5,1 in the order in which they appear