sql 个子句对部分字符串使用 HAVING 截断值错误

sql clauses using HAVING truncated value error for part of the string

我收到一个整数值错误,但不明白为什么。

Warning: #1292 Truncated incorrect INTEGER value: '%accepted%';

Warning: #1292 Truncated incorrect INTEGER value: '%pending%' .

没有处理错误。 “第一位置”的内容没有错误并不重要。

有人可以帮忙吗?

SELECT a.`post_id`, b.`name`,
       MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) as  'Email',
FROM table_1 a

INNER JOIN table_2 b
ON FIND_IN_SET(a.post_id, b.payment_ids)
GROUP BY a.post_id
HAVING OrderStatus LIKE '%processing%' OR '%pending%' OR '%accepted%' AND DeliveryDate >= (DATE_SUB(CURDATE(), INTERVAL 7 DAY)) AND DeliveryType = 'pickup'
SELECT a.`post_id`, b.`name`,
       MAX(case when meta_key = 'value' THEN `meta_value` ELSE NULL END) as  'Email',
FROM table_1 a

INNER JOIN table_2 b
ON FIND_IN_SET(a.post_id, b.payment_ids)
GROUP BY a.post_id
HAVING OrderStatus LIKE '%processing%' OR OrderStatus LIKE '%pending%' OR OrderStatus LIKE '%accepted%' AND DeliveryDate >= (DATE_SUB(CURDATE(), INTERVAL 7 DAY)) AND DeliveryType = 'pickup'

OrderStatus LIKE '%processing%' OR '%pending%' OR '%accepted%'

这不是你想的那样。 MySQL理解为:

(OrderStatus LIKE '%processing%')
OR ('%pending%')
OR ('%accepted%')

因此它尝试在布尔上下文中评估字符串,这会产生您收到的警告。

您需要为每个匹配的字符串重复 like 表达式:

(
    OrderStatus LIKE '%processing%' 
    OR OrderStatus LIKE '%pending%' 
    OR OrderStatus LIKE '%accepted%'
)

或者您可以使用正则表达式:

OrderStatus RLIKE 'processing|pending|accepted'

请注意,这些条件应属于 where 子句而不是 having 子句,因为它们与 non-aggregated 列相关。我会将查询表述为:

select
    t1.post_id,
    t2.name,
    -- no need for an "else" branch here
    max(case when meta_key = 'value' then meta_value end) as email 
from table_1 t1
inner join table_2 t2 on find_in_set(t1.post_id, t2.payment_ids)
where
-- filtering in the "where" clause
    OrderStatus rlike 'processing|pending|accepted'    -- regex
    and DeliveryDate >= current_date - interval 7 day  -- no need for "datesub()"
    and DeliveryType = 'pickup'
group by 
-- all non-aggregated column in the `group by` clause    
    t1.post_id, 
    t2.name  

请注意,您应该在查询中为 所有 列添加它们所属的 table 前缀,以使代码明确且 self-explanatory.