如何在 MySQL 中搜索以 A 開頭的名稱?
為此,請使用 LIKE,如下所示 −
select *from yourTableName where yourColumnName LIKE 'A%';
我們先建立一個表 -
mysql> create table DemoTable ( StudentName varchar(100) ); Query OK, 0 rows affected (0.66 sec)
現在,可以使用 insert 命令在表中插入一些記錄 −
mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Adam Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Aaron Taylor'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.27 sec)
使用 select 語句顯示錶中的所有記錄 −
mysql> select *from DemoTable;
輸出
+--------------+ | StudentName | +--------------+ | John Smith | | Adam Smith | | Aaron Taylor | | Chris Brown | +--------------+ 4 rows in set (0.00 sec)
以下是 MySQL 中查詢以 A 開頭的名稱的查詢 −
mysql> select *from DemoTable where StudentName LIKE 'A%';
輸出
+--------------+ | StudentName | +--------------+ | Adam Smith | | Aaron Taylor | +--------------+ 2 rows in set (0.00 sec)
廣告