我正在尝试为mysql (Search Module)
编写查询,由于要在同一张表中进行选择操作,因此我遇到了困难。
我的查询:
(SELECT * FROM user WHERE `user_name` like '%TOM%' OR `user_name` like '%AN%'
and `login_datetime` BETWEEN '2013-01-01 00:00:00' and '2013-02-31 23:59:59')
OR
(SELECT * FROM user WHERE `user_name` like '%PHP%' OR `user_name` like '%BA%'
and `login_datetime` BETWEEN '2013-02-01 00:00:00' and '2013-03-31 23:59:59')
AND
(SELECT * FROM user WHERE `user_name` like '%SUN%' OR `user_name` like '%MOON%'
and `login_datetime` BETWEEN '2013-03-01 00:00:00' and '2013-04-31 23:59:59')
NAND
(SELECT * FROM user WHERE `user_name` like '%RAJ%' OR `user_name` like '%MUTH%'
and `login_datetime` BETWEEN '2013-04-01 00:00:00' and '2013-06-31 23:59:59')
NOR
(SELECT * FROM user WHERE `user_name` like '%BAG%' OR `user_name` like '%LAP%'
and `login_datetime` BETWEEN '2013-05-01 00:00:00' and '2013-07-31 23:59:59')
上面提到的查询是我的情况。.在无法更改的地方,我试图为此编写一个查询。
注意:
上面的查询在单个表中执行。
OR,NAND,NOR和AND操作可以根据情况进行互换。
这取决于用户,他需要如何搜索。
我尝试了多种方法来实现逻辑,但是我在各个阶段都面临着问题。
检查我的尝试:
MySQL IN BETWEEN with no condition
Doctrine Query from Mysql
Select Query with OR and NAND
最佳答案
NAND和NOR操作可以在SQL中进行如下操作:link
只需根据需要编辑以下查询。
SELECT * FROM user WHERE
(`user_name` like '%TOM%' OR `user_name` like '%AN%' and `login_datetime` BETWEEN '2013-01-01 00:00:00' and '2013-02-31 23:59:59') OR
NOT ( --NOR
(`user_name` like '%PHP%' OR `user_name` like '%BA%' and `login_datetime` BETWEEN '2013-02-01 00:00:00' and '2013-03-31 23:59:59') OR
(`user_name` like '%SUN%' OR `user_name` like '%MOON%' and `login_datetime` BETWEEN '2013-03-01 00:00:00' and '2013-04-31 23:59:59')
) OR
NOT ( --NAND
(`user_name` like '%RAJ%' OR `user_name` like '%MUTH%' and `login_datetime` BETWEEN '2013-04-01 00:00:00' and '2013-06-31 23:59:59') AND
(`user_name` like '%BAG%' OR `user_name` like '%LAP%' and `login_datetime` BETWEEN '2013-05-01 00:00:00' and '2013-07-31 23:59:59')
)
在上面的示例中,返回了所有记录,其中第一括号之间的条件为true或(第二条件NOR第三条件)或(第四条件NAND第五条件)。
注意:条件是指括号之间的子句。
关于mysql - SELECT .. mysql中的SELECT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17044223/