要实现类似MySQL IN()的查询,您需要使用COUNT(),IF()以及LIKE运算符。让我们首先创建一个表-
create table DemoTable -> ( -> Subject varchar(80) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable values('MySQLMongoDB'); insert into DemoTable values('MySQL'); insert into DemoTable values('JavaMySQL'); insert into DemoTable values('MongoDB'); insert into DemoTable values('Java');
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+--------------+ | Subject | +--------------+ | MySQLMongoDB | | MySQL | | JavaMySQL | | MongoDB | | Java | +--------------+ 5 rows in set (0.00 sec)
这是将LIKE实现为IN()的查询-
select -> count(if(Subject like '%MySQL%',1,NULL)) as MySQLCount, -> count(if(Subject like '%Java%',1,NULL)) as JavaCount, -> count(if(Subject like '%MongoDB%',1,NULL)) as MongoDBCount -> from DemoTable;
这将产生以下输出-
+------------+-----------+--------------+ | MySQLCount | JavaCount | MongoDBCount | +------------+-----------+--------------+ | 3 | 2 | 2 | +------------+-----------+--------------+ 1 row in set (0.00 sec)