關注 wx:CodingTechWork
需求
在進行一些產品開發時,需要在一個填寫框中填寫名稱或者編碼進行精準或模糊查詢。要求:
- 一個填寫框供底層數據表結構多個字段進行模糊查詢使用。
- 查詢結果排序:名稱優先排序,然後按照編碼排序,若查不到按照時間排序。
實踐
方法
使用order by
配合case when...then...語句
。
建表
CREATE TABLE `t1` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`code` varchar(255) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
插入
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (1, '中國蘇州', 'chinasz', '2023-03-13 11:20:51');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (2, '中國蘇州常熟', 'chinaszcs', '2023-03-13 11:22:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (3, '中國江蘇', 'chinajs', '2023-03-11 11:20:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (4, '江蘇', 'js', '2023-03-12 11:20:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (5, '中國江蘇鹽城', 'chinajsyc', '2023-03-13 11:10:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (6, '江蘇鹽城', 'jsyc', '2023-03-13 11:11:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (7, '中國江蘇無錫', 'chinajswx', '2023-03-10 11:20:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (8, '江蘇常州', 'jscz', '2023-02-13 11:20:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (9, '中國', 'china', '2023-01-13 11:20:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (10, '江蘇蘇州', 'jssz', '2023-03-11 11:20:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (11, '江浙滬', 'jzh', '2023-03-13 11:20:40');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (12, '江蘇001', 'js001', '2023-03-11 11:20:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (13, '中國江蘇省', 'chinajss', '2023-03-13 11:20:52');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (14, '江蘇省', 'jss', '2023-03-13 12:19:03');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (15, '新加坡', 'singapore', '2023-03-13 12:19:17');
INSERT INTO `testdb`.`t1` (`id`, `name`, `code`, `create_time`) VALUES (16, '日本', 'japan', '2023-03-13 12:19:37');
排序查詢
全局查詢
sql語句
select * from t1 group by id ORDER BY create_time DESC;
查詢結果
mysql> select * from t1 group by id ORDER BY create_time DESC;
+----+--------------------+-----------+---------------------+
| id | name | code | create_time |
+----+--------------------+-----------+---------------------+
| 16 | 日本 | japan | 2023-03-13 12:19:37 |
| 15 | 新加坡 | singapore | 2023-03-13 12:19:17 |
| 14 | 江蘇省 | jss | 2023-03-13 12:19:03 |
| 2 | 中國蘇州常熟 | chinaszcs | 2023-03-13 11:22:52 |
| 13 | 中國江蘇省 | chinajss | 2023-03-13 11:20:52 |
| 1 | 中國蘇州 | chinasz | 2023-03-13 11:20:51 |
| 11 | 江浙滬 | jzh | 2023-03-13 11:20:40 |
| 6 | 江蘇鹽城 | jsyc | 2023-03-13 11:11:52 |
| 5 | 中國江蘇鹽城 | chinajsyc | 2023-03-13 11:10:52 |
| 4 | 江蘇 | js | 2023-03-12 11:20:52 |
| 3 | 中國江蘇 | chinajs | 2023-03-11 11:20:52 |
| 10 | 江蘇蘇州 | jssz | 2023-03-11 11:20:52 |
| 12 | 江蘇001 | js001 | 2023-03-11 11:20:52 |
| 7 | 中國江蘇無錫 | chinajswx | 2023-03-10 11:20:52 |
| 8 | 江蘇常州 | jscz | 2023-02-13 11:20:52 |
| 9 | 中國 | china | 2023-01-13 11:20:52 |
+----+--------------------+-----------+---------------------+
16 rows in set (0.00 sec)
不輸入參數查詢
sql語句
set @a = '';
select t.id, t.`name`,t.`code`,t.create_time
from t1 t where t.`name` like concat('%',@a,'%')
or t.`code` like concat('%',@a,'%') group by t.id
order by(
case
when t.`name` = @a then 1
when t.`code` = @a then 2
when t.`name` like concat(@a,'%') then 3
when t.`name` like concat('%',@a,'%') then 4
when t.`name` like concat('%',@a) then 5
when t.`code` like concat(@a,'%') then 6
when t.`code` like concat('%',@a,'%') then 7
when t.`code` like concat(@a,'%') then 8
else 9
end)
asc, t.create_time desc;
+----+--------------------+-----------+---------------------+
| id | name | code | create_time |
+----+--------------------+-----------+---------------------+
| 16 | 日本 | japan | 2023-03-13 12:19:37 |
| 15 | 新加坡 | singapore | 2023-03-13 12:19:17 |
| 14 | 江蘇省 | jss | 2023-03-13 12:19:03 |
| 2 | 中國蘇州常熟 | chinaszcs | 2023-03-13 11:22:52 |
| 13 | 中國江蘇省 | chinajss | 2023-03-13 11:20:52 |
| 1 | 中國蘇州 | chinasz | 2023-03-13 11:20:51 |
| 11 | 江浙滬 | jzh | 2023-03-13 11:20:40 |
| 6 | 江蘇鹽城 | jsyc | 2023-03-13 11:11:52 |
| 5 | 中國江蘇鹽城 | chinajsyc | 2023-03-13 11:10:52 |
| 4 | 江蘇 | js | 2023-03-12 11:20:52 |
| 3 | 中國江蘇 | chinajs | 2023-03-11 11:20:52 |
| 10 | 江蘇蘇州 | jssz | 2023-03-11 11:20:52 |
| 12 | 江蘇001 | js001 | 2023-03-11 11:20:52 |
| 7 | 中國江蘇無錫 | chinajswx | 2023-03-10 11:20:52 |
| 8 | 江蘇常州 | jscz | 2023-02-13 11:20:52 |
| 9 | 中國 | china | 2023-01-13 11:20:52 |
+----+--------------------+-----------+---------------------+
16 rows in set (0.00 sec)
輸入查詢參數
sql語句
set @a = 'js';
select t.id, t.`name`,t.`code`,t.create_time
from t1 t where t.`name` like concat('%',@a,'%')
or t.`code` like concat('%',@a,'%') group by t.id
order by(
case
when t.`name` = @a then 1
when t.`code` = @a then 2
when t.`name` like concat(@a,'%') then 3
when t.`name` like concat('%',@a,'%') then 4
when t.`name` like concat('%',@a) then 5
when t.`code` like concat(@a,'%') then 6
when t.`code` like concat('%',@a,'%') then 7
when t.`code` like concat(@a,'%') then 8
else 9
end)
asc, t.create_time desc;
結果
+----+--------------------+-----------+---------------------+
| id | name | code | create_time |
+----+--------------------+-----------+---------------------+
| 4 | 江蘇 | js | 2023-03-12 11:20:52 |
| 14 | 江蘇省 | jss | 2023-03-13 12:19:03 |
| 6 | 江蘇鹽城 | jsyc | 2023-03-13 11:11:52 |
| 10 | 江蘇蘇州 | jssz | 2023-03-11 11:20:52 |
| 12 | 江蘇001 | js001 | 2023-03-11 11:20:52 |
| 8 | 江蘇常州 | jscz | 2023-02-13 11:20:52 |
| 13 | 中國江蘇省 | chinajss | 2023-03-13 11:20:52 |
| 5 | 中國江蘇鹽城 | chinajsyc | 2023-03-13 11:10:52 |
| 3 | 中國江蘇 | chinajs | 2023-03-11 11:20:52 |
| 7 | 中國江蘇無錫 | chinajswx | 2023-03-10 11:20:52 |
+----+--------------------+-----------+---------------------+
10 rows in set (0.00 sec)