背景:你們公司超級注重企業文化,要求大家要做好孩子多讀書,公司老闆叫王富貴,老闆娘叫張翠花,另有員工若幹人。
需求:領導要求搞一個員工讀書記錄排名,展示出每個員工所讀過的書都有啥?
分析:從這樣及其常見的一句話需求描述中,聰明的小趴菜你肯定知道,老闆肯定要排在最前面,然後應該是老闆娘,剩餘的普通員工按照姓名首字母排等等這些隐藏的需求。
方案:
首先給出數據建表腳本(Oracle)
CREATE TABLE T_BOOK_RECORD (
ID VARCHAR2(100) NOT NULL,
USER_NAME VARCHAR2(100) NOT NULL,
BOOK_NAME VARCHAR2(100) NOT NULL,
CONSTRAINT T_BOOK_RECORD_PK PRIMARY KEY (ID)
);
COMMENT ON TABLE T_BOOK_RECORD IS '閱讀記錄';
COMMENT ON COLUMN T_BOOK_RECORD.ID IS '主鍵';
COMMENT ON COLUMN T_BOOK_RECORD.USER_NAME IS '人員姓名';
COMMENT ON COLUMN T_BOOK_RECORD.BOOK_NAME IS '書籍名稱';
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('1', '王富貴', '《如何合法罰款》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('2', '李加成', '《舔狗的自我修養》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('3', '孫正易', '《囚徒健身插圖版》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('4', '王絲匆', '《窮爸爸富爸爸》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('5', '趙僞', '《資本遊戲》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('6', '王富貴', '《人家大愛-996福報》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('7', '張翠花', '《拴住老公的胃》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('8', '張翠花', '《老闆娘不是老闆的娘》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('9', '李加成', '《老子明天不上班》');
INSERT INTO T_BOOK_RECORD (ID, USER_NAME, BOOK_NAME) VALUES('10', '王絲匆', '《投胎那些事》');
SELECT USER_NAME ,BOOK_NAME FROM T_BOOK_RECORD ORDER BY NLSSORT(USER_NAME,'NLS_SORT = SCHINESE_PINYIN_M')
得到結果如下:
public List<BookRecord> list(){
List<BookRecord> boss = new ArrayList<>();
List<BookRecord> bossWife = new ArrayList<>();
List<BookRecord> nobody = new ArrayList<>();
for(BookRecord br : list){
if ("王富貴".equals(br.getUserName())){
boss.add(br);
}else if ("張翠花".equals(br.getUserName())){
bossWife.add(br);
}else {
nobody.add(br);
}
}
boss.addAll(bossWife);
boss.addAll(nobody);
return boss;
}
SELECT * FROM T_BOOK_RECORD ORDER BY decode(USER_NAME,'王富貴',1,'張翠花',2) ,NLSSORT(USER_NAME,'NLS_SORT = SCHINESE_PINYIN_M')
得到結果如下:
這裡主要有以下幾個知識點:
SELECT * FROM T_BOOK_RECORD ORDER BY NLSSORT(USER_NAME,'NLS_SORT = SCHINESE_PINYIN_M')
DECODE(USER_NAME,'王富貴',1,'張翠花',2),這個代表,如果USER_NAME='王富貴',函數返回1,如果USER_NAME='張翠花',函數返回2,這個函數的返回值是優先級最高的排序條件
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!