Home > Backend Development > PHP Tutorial > mysql怎么按特定id排序

mysql怎么按特定id排序

WBOY
Release: 2016-06-13 11:20:48
Original
827 people have browsed it

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');
Copy after login


------解决方案--------------------
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 
Copy after login

find_in_set(p.id, p_sort.best_sort_person_id)>0 desc 用于将id=2,5,1的排在前面
find_in_set(p.id, p_sort.best_sort_person_id) asc 用于将id=2,5,1的按出现次序排列

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template