mysql如何按特定id排序
SET FOREIGN_KEY_CHECKS=0;-- ----------------------------
-- Table structure for `p`
-- ----------------------------
DROP TABLE IF EXISTS `p`;
CREATE TABLE `p` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `categories_id` int(11) 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(10) NOT NULL default '0',
  `best_sort_person_id` varchar(100) default NULL,
  PRIMARY KEY  (`categories_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of p_sort
-- ----------------------------
INSERT INTO `p_sort` VALUES ('2', '2,5,1');

解决方案 »

  1.   

    select p.*
    from p,p_sort
    where p.categories_id=p_sort.categories_id
    And p.categories_id=2
    order by INSTR(p_sort.best_sort_person_id,p.id);
      

  2.   

    mysql> select p.*  from p,p_sort where p.categories_id=p_sort.categories_id And p.categories_id=2 order by case when INSTR(p_sort.best_sort_person_id,p.id)=0 then 100 else INSTR(p_sort.best_sort_person_id,p.id) end ;                                       
    +----+--------+---------------+
    | id | name   | categories_id |
    +----+--------+---------------+
    |  2 | tina   |             2 |
    |  5 | world  |             2 |
    |  1 | jimmy  |             2 |
    |  3 | dd     |             2 |
    |  4 | hello  |             2 |
    |  6 | slucky |             2 |
    +----+--------+---------------+
    6 rows in set (0.00 sec)