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)