您可以CONCAT()
为此使用功能。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value1 varchar(10), Value2 varchar(10) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Value1,Value2) values('10','345'); mysql> insert into DemoTable(Value1,Value2) values('14','789'); mysql> insert into DemoTable(Value1,Value2) values('18','234');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+--------+--------+ | Id | Value1 | Value2 | +----+--------+--------+ | 1 | 10 | 345 | | 2 | 14 | 789 | | 3 | 18 | 234 | +----+--------+--------+ 3 rows in set (0.00 sec)
以下是在单个查询中将列与LIKE匹配之前合并列的查询-
mysql> SELECT * FROM DemoTable WHERE CONCAT(Value1, Value2) LIKE '%147%';
这将产生以下输出-
+----+--------+--------+ | Id | Value1 | Value2 | +----+--------+--------+ | 2 | 14 | 789 | +----+--------+--------+ 1 row in set (0.00 sec)