mysql如何按特定id排序 我想讲数据按某些特定id排前面,怎么做到?
Sql代码 收藏代码
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');
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
select e.* from (select a.* from p a,p_sort b where a.categories_id = b.categories_id and find_in_set(a.id,b.best_sort_person_id) order by find_in_set(a.id,b.best_sort_person_id)) e union
select a.* from p a,p_sort b where a.categories_id = b.categories_id and not find_in_set(a.id,b.best_sort_person_id)