要获取以特定字符开头的名称,您需要使用LIKE。让我们首先创建一个表:
mysql> create table DemoTable ( StudentFirstName varchar(20) );
以下是使用insert命令在表中插入一些记录的查询:
mysql> insert into DemoTable values('John'); mysql> insert into DemoTable values('Carol'); mysql> insert into DemoTable values('Johnny'); mysql> insert into DemoTable values('Robert'); mysql> insert into DemoTable values('Chris'); mysql> insert into DemoTable values('Ramit');
以下是使用select命令显示表中记录的查询:
mysql> select *from DemoTable;
这将产生以下输出:
+------------------+ | StudentFirstName | +------------------+ | John | | Carol | | Johnny | | Robert | | Chris | | Ramit | +------------------+ 6 rows in set (0.00 sec)
以下是将第一个字符与LIKE进行匹配的查询:即,所有姓氏以字符C开头的学生:
mysql> select *from DemoTable where StudentFirstName LIKE 'C%';
这将产生以下输出:
+------------------+ | StudentFirstName | +------------------+ | Carol | | Chris | +------------------+ 2 rows in set (0.00 sec)