强调内容

题目:按照姓名降序排列,并且实现 NUM>0的记录放在最前面,且也是按照姓名降序排列的;

DROP TABLE IF EXISTS stud_name;
CREATE TABLE stud_name (
NAME varchar(30) collate utf8_bin default NULL,
NUM int(8) default NULL,
ID bigint(10) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
– Records
INSERT INTO stud_name VALUES (‘b’, ‘2’, ‘4’);
INSERT INTO stud_name VALUES (‘e’, ‘4’, ‘5’);
INSERT INTO stud_name VALUES (‘d’, ‘0’, ‘6’);
INSERT INTO stud_name VALUES (‘a’, ‘5’, ‘7’);
INSERT INTO stud_name VALUES (‘c’, ‘4’, ‘8’);
INSERT INTO stud_name VALUES (‘b’, ‘2’, ‘9’);
INSERT INTO stud_name VALUES (‘a’, ‘1’, ‘10’);
INSERT INTO stud_name VALUES (‘a’, ‘0’, ‘11’);

解析过程:

通过给NAME+NUM加权值 weight 实现排序规则,再利用 order by weight 实现题目要求;