MySQL 查询使用 LIKE 运算符在多个字段上查找相同的值?
mysqlmysqli database
首先我们创建一个表 −
mysql> create table DemoTable -> -> ( -> StudentName varchar(20), -> StudentSubject varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('John','MySQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam','MySQL'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('John','Java'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values('Carol','Java'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('John','MongoDB'); Query OK, 1 row affected (0.12 sec)
使用 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)