如何获得 MySQL 组合字段结果?
您可以为此使用 MySQL 中的 CONCAT() 函数。我们首先创建一个表 -
mysql> create table DemoTable ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientFirstName varchar(20), ClientLastName varchar(20) ); Query OK, 0 rows affected (0.50 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable(ClientFirstName,ClientLastName) values('John','Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ClientFirstName,ClientLastName) values('John','Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(ClientFirstName,ClientLastName) values('Carol','Taylor'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ClientFirstName,ClientLastName) values('Adam','Smith'); Query OK, 1 row affected (0.13 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+----------+-----------------+----------------+ | ClientId | ClientFirstName | ClientLastName | +----------+-----------------+----------------+ | 1 | John | Smith | | 2 | John | Miller | | 3 | Carol | Taylor | | 4 | Adam | Smith | +----------+-----------------+----------------+ 4 rows in set (0.00 sec)
以下是获取组合字段结果的查询 -
mysql> select ClientId from DemoTable where concat(ClientFirstName,' ',ClientLastName) LIKE 'Jo%';
这将产生以下输出 -
+----------+ | ClientId | +----------+ | 1 | | 2 | +----------+ 2 rows in set (0.00 sec)
广告