Mysql sql子句使用的部分字符串具有截断值错误
我得到一个整数值错误,但不明白为什么 警告:#1292截断了不正确的整数值:“%accepted%” 警告:#1292截断了不正确的整数值:“%pending%” 它没有用于处理的错误。“第一位置”中的内容没有错误并不重要 有人能帮忙吗Mysql sql子句使用的部分字符串具有截断值错误,mysql,sql,where-clause,sql-like,having-clause,Mysql,Sql,Where Clause,Sql Like,Having Clause,我得到一个整数值错误,但不明白为什么 警告:#1292截断了不正确的整数值:“%accepted%” 警告:#1292截断了不正确的整数值:“%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
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'
OrderStatus,如“%processing%”或“%pending%”或“%accepted%”
这和你想的不一样。MySQL将其理解为:
(OrderStatus LIKE '%processing%')
OR ('%pending%')
OR ('%accepted%')
因此,它尝试在布尔上下文中计算字符串,这会产生您正在得到的警告
您需要为每个匹配的字符串重复like
表达式:
(
OrderStatus LIKE '%processing%'
OR OrderStatus LIKE '%pending%'
OR OrderStatus LIKE '%accepted%'
)
或者您可以使用regexp:
OrderStatus RLIKE 'processing|pending|accepted'
请注意,这些条件应属于where
子句,而不是having
子句,因为它们与非聚合列相关。我想把这个问题表述为:
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
请注意,您应该在查询中的所有列前面加上它们所属的表,以使代码清晰明了。您不能只使用或值。您需要有完整的语句,所以像。。。或者OrderStatus,比如…啊,非常感谢。谢谢,有人告诉我我不能使用WHERE子句,并且当我尝试使用它时产生了错误。是的,我把它放在适当的地方了。它只会提供一个空的结果集。在继续播放时,它会提供一个警告:#1292在发布空结果集时截断了不正确的日期值:“DeliveryDate”。以前我有WHERE子句,但是在这篇文章中被更正了-[链接]@Tim:这不是我给你的查询。但可能在列名
DeliveryDate
周围有单引号,但不应该在那里。正确。如果我没有引号,我会得到一个错误#1054-where子句中的未知列'DeliveryType'Correct。如果我没有引号,我会得到一个错误#1054-where子句中的未知列“DeliveryType”-Tim
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