首页 > 解决方案 > MySQL where 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

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

标签: mysqlsqldatewhere-clausesql-like

解决方案


它应该返回在提供的日期/时间段之间具有 %bob% 的记录。

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

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

推荐阅读