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)