MySQL查询使用LIKE运算符在多个字段上找到相同的值?
让我们首先创建一个表-
mysql> create table DemoTable -> -> ( -> StudentName varchar(20), -> StudentSubject varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('John','MySQL'); mysql> insert into DemoTable values('Adam','MySQL'); mysql> insert into DemoTable values('John','Java'); mysql> insert into DemoTable values('Carol','Java'); mysql> insert into DemoTable values('John','MongoDB');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------------+----------------+ | StudentName | StudentSubject | +-------------+----------------+ | John | MySQL | | Adam | MySQL | | John | Java | | Carol | Java | | John | MongoDB | +-------------+----------------+ 5 rows in set (0.00 sec)
这是使用类似运算符在多个字段上查找相同值的查询-
mysql> select *from DemoTable -> where concat(StudentName,' ',StudentSubject) -> like '%John MySQL%';
这将产生以下输出-
+-------------+----------------+ | StudentName | StudentSubject | +-------------+----------------+ | John | MySQL | +-------------+----------------+ 1 row in set (0.00 sec)