MySQL 選擇語句用於獲取字串左邊的 5 個字元
要獲取從字串左側開始指定個數的字元,請使用 MySQL 中的 LEFT 方法。我們首先建立一個表 -
mysql> create table DemoTable ( Name varchar(100) ); Query OK, 0 rows affected (6.58 sec)
使用插入命令在表中插入一些記錄 -
mysql> insert into DemoTable values('Sam Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Adam Smith'); Query OK, 1 row affected (7.01 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.68 sec)
使用選擇語句顯示錶中的所有記錄 -
mysql> select *from DemoTable;
這將產生以下輸出 -
+--------------+ | Name | +--------------+ | Sam Brown | | David Miller | | Adam Smith | | Carol Taylor | +--------------+ 4 rows in set (0.00 sec)
以下是選擇從字串左側獲取 5 個字元的 SELECT 語句 -
mysql> select left(Name,5) from DemoTable order by Name;
這將產生以下輸出 -
+--------------+ | left(Name,5) | +--------------+ | Adam | | Carol | | David | | Sam B | +--------------+ 4 rows in set (0.46 sec)
廣告