mysql,其中介于

kmynzznz  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(240)

下面是查询示例:

select
    email,
    firstName,
    lastName
from
    users
where
    createdAt between '2020-04-01 00:00:00.0' and '2020-09-30 23:59:59.0'
    or email like '%bob%'
    or firstName like '%bob%'
    or lastName like '%bob%'
order by
    createdAt desc

我得到的结果是:

remi10@martin10.com,remi10,martin10
remi6@martin6.com,Bobby,martin6
remi5@gmail.com,Bob,martin5
remi4@gmail.com,remi4,martin4

我在试着。。。

remi6@martin6.com,Bobby,martin6
remi5@gmail.com,Bob,martin5

它应该返回包含 %bob% 在提供的日期/时间段之间。

i2byvkas

i2byvkas1#

它应该返回在提供的日期/时间段之间具有%bob%的记录。
你需要 and 在日期过滤器和多个相似条件之间。我还建议使用半开区间过滤来简化日期过滤器:

where
    createdAt >= '2020-04-01' 
    and createdAt < '2020-10-01'
    and (
       email like '%bob%'
        or firstName like '%bob%'
        or lastName like '%bob%'
   )
0h4hbjxa

0h4hbjxa2#

首先,我建议简化数据逻辑。第二,你需要括号:

where (createdAt >= '2020-04-01' and
       createdAt < '2020-10-01'
       ) and
       ( email like '%bob%' or
         firstName like '%bob%' or
         lastName like '%bob%' 
       )

相关问题