mysql使用SUBSTRING展示特定字段裏面的特定字符
阿新 • • 發佈:2018-10-15
ins ble def sub tab engine 層級 into unicode 場景: 測試人員需要查詢特定字段裏裏面的特定字符串,但是正則不會寫,看到了SUBSTRING來截取特定字符串,感覺不錯。
2:插入數據
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO .
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO A:取path的字段的後面三位
select SUBSTRING(path, -3) from erp_orgs;
-2-
-3-
-4-
-5-
-6-
-7-
-8-
-9-
10- (未知結果 後續查找)
(重點: 如果字段長度不足 展示結果為空)
B:從左開始第3位取(包括第三位)
select SUBSTRING(path, 3) from erp_orgs;
C:取左邊的3位
select SUBSTRING(path, 1,3) from erp_orgs;
這種只能針對固定長度,比說url共8個字符,可以下面這種寫法
select SUBSTRING(path,length(
4:正式查詢 例如查詢後面的數字為id的 一般後面都帶有 - 字符
SELECT * from erp_orgs WHERE SUBSTRING(path,-2) = id;
1:數據如下
CREATE TABLE erp_orgs
(id
int(10) UNSIGNED NOT NULL AUTO_INCREMENT,path
varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT ‘-‘ COMMENT ‘層級路徑‘,
PRIMARY KEY (id
) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5044 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (1, ‘1-‘);INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (2, ‘1-2-‘);INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (3, ‘1-2-3-‘);INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (4, ‘1-2-4-‘);INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (5, ‘1-2-5-‘);INSERT INTO
hanye
erp_orgs
(id
, path
) VALUES (6, ‘1-2-4-6-‘);INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (7, ‘1-2-3-7-‘);INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (8, ‘1-2-3-8-‘);INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (9, ‘1-2-3-7-9-‘);INSERT INTO
hanye
.erp_orgs
(id
, path
) VALUES (10, ‘1-2-3-7-10-‘);
3:開始查詢自己需要的字段
select SUBSTRING(path, -3) from erp_orgs;
-2-
-3-
-4-
-5-
-6-
-7-
-8-
-9-
10- (未知結果 後續查找)
(重點: 如果字段長度不足 展示結果為空)
B:從左開始第3位取(包括第三位)
select SUBSTRING(path, 3) from erp_orgs;
C:取左邊的3位
select SUBSTRING(path, 1,3) from erp_orgs;
這種只能針對固定長度,比說url共8個字符,可以下面這種寫法
select SUBSTRING(path,length(
url
)-5) from erp_orgs;4:正式查詢 例如查詢後面的數字為id的 一般後面都帶有 - 字符
SELECT * from erp_orgs WHERE SUBSTRING(path,-2) = id;
mysql使用SUBSTRING展示特定字段裏面的特定字符