mysql如何按特定id排序
mysql如何按特定id排序
SET FOREIGN_KEY_CHECKS=0;<br /> <br /> -- ----------------------------<br /> -- Table structure for `p`<br /> -- ----------------------------<br /> DROP TABLE IF EXISTS `p`;<br /> CREATE TABLE `p` (<br /> `id` int(11) NOT NULL auto_increment,<br /> `name` varchar(255) default NULL,<br /> `categories_id` int(11) default NULL,<br /> PRIMARY KEY (`id`)<br /> ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;<br /> <br /> -- ----------------------------<br /> -- Records of p<br /> -- ----------------------------<br /> INSERT INTO `p` VALUES ('1', 'jimmy', '2');<br /> INSERT INTO `p` VALUES ('2', 'tina', '2');<br /> INSERT INTO `p` VALUES ('3', 'dd', '2');<br /> INSERT INTO `p` VALUES ('4', 'hello', '2');<br /> INSERT INTO `p` VALUES ('5', 'world', '2');<br /> INSERT INTO `p` VALUES ('6', 'slucky', '2');<br /> <br /> <br /> SET FOREIGN_KEY_CHECKS=0;<br /> <br /> -- ----------------------------<br /> -- Table structure for `p_sort`<br /> -- ----------------------------<br /> DROP TABLE IF EXISTS `p_sort`;<br /> CREATE TABLE `p_sort` (<br /> `categories_id` int(10) NOT NULL default '0',<br /> `best_sort_person_id` varchar(100) default NULL,<br /> PRIMARY KEY (`categories_id`)<br /> ) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br /> <br /> -- ----------------------------<br /> -- Records of p_sort<br /> -- ----------------------------<br /> INSERT INTO `p_sort` VALUES ('2', '2,5,1');
select * from p, p_sort<br /> order by find_in_set(p.id, p_sort.best_sort_person_id)>0 desc, find_in_set(p.id, p_sort.best_sort_person_id) asc, id