gpt4 book ai didi

mysql - Mysql 中的 SELECT .. SELECT

转载 作者:行者123 更新时间:2023-11-29 13:48:38 26 4
gpt4 key购买 nike

我正在尝试为 mysql (搜索模块)编写查询,但由于要在同一个表内执行选择操作,我遇到了困难。

我的查询:

(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')

上面提到的查询是我的场景。如果无法更改,我正在尝试为此编写一个查询。

注意:

  1. 上述查询在单个表中执行。
  2. OR、NAND、NOR、AND运算可以根据情况互换。
  3. 这取决于用户,他需要如何搜索。

我尝试了很多方法来实现逻辑,但我在所有阶段都面临问题。

检查我的尝试: 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 OR(第二个条件 NOR 3rd 条件)OR(第四个条件 NAND 第五个条件)的所有记录。

注意:条件是指括号内的子句。

关于mysql - Mysql 中的 SELECT .. SELECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17044223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com