我有一个简单的 EAV 之类的数据库,其中有 3 个表:产品、属性、attribute_values。
我使用下面的代码来查找具有“长度”属性的所有产品,然后使用子字符串函数来删除“英寸”,之后我使用 Between 来获取“长度”在 0 之间的所有产品和 5。
SELECT products.name,products.id
FROM products
JOIN attribute_value_product on products.id = attribute_value_product.product_id
JOIN attribute_values on attribute_value_product.attribute_value_id = attribute_values.id
JOIN attributes on attribute_values.attribute_id = attributes.id AND attributes.name = 'length'
WHERE substring_index(attribute_values.value,'inch',1) + 0 BETWEEN 0 AND 5
一切正常,但我收到了大量警告:
Warning: #1292 Truncated incorrect DOUBLE value: 'black'
Warning: #1292 Truncated incorrect DOUBLE value: 'phat farm'
Warning: #1292 Truncated incorrect DOUBLE value: '2 kg'
Warning: #1292 Truncated incorrect DOUBLE value: 'roca wear'
在我看来,该子字符串正在遍历数据库中的所有 attribute_values,即使是那些没有引用属性“长度”的值 - 是否可以更改它?
最佳答案
SUBSTRING_INDEX() 函数返回字符串的子字符串 那么你的where条件
WHERE substring_index(attribute_values.value,'inch',1) + 0 BETWEEN 0 AND 5
错误,因为您尝试将字符串添加到数字
可能您正在寻找子字符串的左侧
WHERE length(substring_index(attribute_values.value,'inch',1)) + 0 BETWEEN 0 AND 5
注意:如果未找到分隔符(“英寸”),则该函数将返回所有字符串,因此您应该检查
似乎您想阅读上一部分:
The SUBSTRING_INDEX() function returns a substring of a string
如果您想要“英寸”之前的字符串数字部分,您可以尝试使用强制转换
WHERE cast(trim(substring_index(attribute_values.value,'inch',1)) AS UNSIGNED ) + 0
BETWEEN 0 AND 5
为了避免出现非数字内容的警告,您可以尝试这个 where 条件
WHERE (case when substring_index(attribute_values.value,'inch',1) = attribute_values.value
then 1000
else cast(trim(substring_index(attribute_values.value,'inch',1)) AS UNSIGNED ) end )
BETWEEN 0 AND 5
关于mysql - 使用子字符串后mysql查询中出现多个警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677238/