使用 LIKE 运算符查找多个字段上具有相同值的 MySQL 查询?
我们首先创建一个表 -
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)
以下是使用 like 运算符查找多个字段上具有相同值的查询 -
mysql> select *from DemoTable -> where concat(StudentName,' ',StudentSubject) -> like '%John MySQL%';
这将生成以下输出 -
+-------------+----------------+ | StudentName | StudentSubject | +-------------+----------------+ | John | MySQL | +-------------+----------------+ 1 row in set (0.00 sec)
广告