MySQL where between and multi or like

MySQL where between and multi or like

示例查询如下:

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

它应该返回在提供的 date/time 期间具有 %bob% 的记录。

It should be returning the records that have the %bob% between the provided date/time period.

您需要 and 在日期过滤器和多个喜欢的条件之间。我还建议使用半开间隔过滤来简化日期过滤器:

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

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

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