iteye_12000 2013-04-15 12:44
浏览 929
已采纳

mysql如何按特定id排序

mysql如何按特定id排序
我想讲数据按某些特定id排前面,怎么做到?

[code="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');

[/code]

[img]http://dl.iteye.com/upload/attachment/0083/1663/37a9d9a5-3461-3568-aff2-fbf4fa17df3b.png[/img]

  • 写回答

2条回答 默认 最新

  • zyn010101 2013-04-15 13:09
    关注

    [code="java"]
    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) [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿