要从字符串的左侧获取字符数,请在MySQL中使用LEFT方法。让我们首先创建一个表-
mysql> create table DemoTable ( Name varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Sam Brown'); mysql> insert into DemoTable values('David Miller'); mysql> insert into DemoTable values('Adam Smith'); mysql> insert into DemoTable values('Carol Taylor');
使用select语句显示表中的所有记录-
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)