如何使用 MySQL SELECT LEFT 獲取包含帶有斜槓字串的記錄


我們首先建立一個 -

mysql> create table DemoTable1418
   -> (
   -> EmployeeCode text
   -> );
Query OK, 0 rows affected (0.51 sec)

使用 insert 在表中插入一些記錄 -

mysql> insert into DemoTable1418 values('EMP-2110/Carol');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1418 values('EMP-1900/David');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1418 values('EMP-2345/Mike');
Query OK, 1 row affected (0.17 sec)

使用 select 從表中顯示所有記錄 -

mysql> select * from DemoTable1418;

將生成以下輸出 -

+----------------+
| EmployeeCode   |
+----------------+
| EMP-2110/Carol |
| EMP-1900/David |
| EMP-2345/Mike  |
+----------------+
3 rows in set (0.00 sec)

這是在 MySQL 中使用 SELECT LEFT 在斜槓前獲取上述記錄的查詢 -

mysql> select left(EmployeeCode,8) from DemoTable1418;

將生成以下輸出 -

+----------------------+
| left(EmployeeCode,8) |
+----------------------+
| EMP-2110             |
| EMP-1900             |
| EMP-2345             |
+----------------------+
3 rows in set (0.00 sec)

更新時間:12-Nov-2019

65 次瀏覽

開啟您的 職業 生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.