简体   繁体   English

使用子字符串后,mysql quert中出现多个警告

[英]Multiple warnings in mysql quert after using substring

I have simple EAV like database which has 3 tables: products, attributes, attribute_values. 我有像数据库这样的简单EAV,它具有3个表:产品,属性,attribute_values。

I am using below code to find all products that have attribute 'length' and then I am using substring function to get rid of 'inch' and after that I am using between to get all products that have 'length' between 0 and 5. 我正在使用下面的代码来查找所有具有“长度”属性的产品,然后使用子字符串函数来消除“英寸”,然后在这之间使用以获得“长度”在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

Everything works fine however I am getting plenty of warnings: 一切正常,但是我收到很多警告:

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'

it looks to me like that substring is going through all attribute_values in the database, even those that are not referring to attribute 'length' - is it possible to change that ? 在我看来,子字符串正在遍历数据库中的所有attribute_values,即使那些未引用属性“ length”的子字符串也可以更改?

The SUBSTRING_INDEX() function returns a substring of a string Then your where condition SUBSTRING_INDEX()函数返回字符串的子字符串,然后返回where条件

WHERE substring_index(attribute_values.value,'inch',1) + 0 BETWEEN 0 AND 5

is wrong because you are try adding a string to a number 是错误的,因为您尝试将字符串添加到数字

could be you are looking for the left of the substr 可能是您正在寻找substr的左侧

WHERE length(substring_index(attribute_values.value,'inch',1)) + 0 BETWEEN 0 AND 5

NOTE: if the delimiter ('inch') is not foudn then the function return all the string so you should check 注意:如果定界符(“ inch”)不是整数,则该函数返回所有字符串,因此您应检查

Seems you want read the previous part : 您似乎要阅读上一部分:

The SUBSTRING_INDEX() function returns a substring of a string SUBSTRING_INDEX()函数返回字符串的子字符串

of if you want the numeric part of the string before the 'inch' you could try uing cast 如果您想让字符串的数字部分在“英寸”之前,可以尝试使用强制类型转换

WHERE cast(trim(substring_index(attribute_values.value,'inch',1)) AS UNSIGNED ) + 0 
            BETWEEN 0 AND 5

and for avoid the warning for not numeric content you could try this where condition 为了避免非数字内容的警告,您可以在有条件的情况下尝试此操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM