我有以下两张表,请参阅图片
我正在尝试比较varchar字段,这是我尝试过的
`select product_id,category_id,color_name,style_name,
case when color_name not in (select attribute_column_value from predefined_attributes where category_id=1 and attribute_column_name='color_name') then 1 else 0 END +
case when style_name not in (select attribute_column_value from predefined_attributes where category_id=1 and attribute_column_name='style_name') then 1 else 0 END as total_mismatch_count
from product_detail where product_id in (123456,234567) and category_id=1;`
这是查询结果,请参阅图片
查询结果
实际上,total_mismatch_count shoud返回0,但它显示2为total_mismatch_count。
我已经使用下面的查询检查了设备color_classs表中的attribute_column_value for color_name和style_name,并且我没有看到输出和product_detail输出之间的任何差异。
`select attribute_column_value from predefined_attributes where category_id=1 and attribute_column_name in ('color_name','style_name') and attribute_column_value in ('Green','Pink','Basic','Classic');`
我做错了什么?,比较varchar的最佳方法是什么,以便mysql查询返回total_mismatch_count为0,这是正确的结果?
注意:我也更新了这两个表,以便它没有任何空格
`update predefined_attributes set attribute_column_value=
trim(replace(replace(replace(attribute_column_value,'\t',''),'\n',''),'\r',''));`
请帮忙。
答案 0 :(得分:0)
我无法测试它,但是类似的东西:
SELECT Count(*) as total_mismatch_count
FROM
predefined_attributes p_a
LEFT JOIN product_detail p_d1 ON p_d1.attribute_column_name = 'color_name'
AND p_d1.category_id = p_a.category_id
AND p_d1.color_name = p_a.attribute_column_value
LEFT JOIN product_detail p_d2 ON p_d2.attribute_column_name = 'style_name'
AND p_d2.category_id = p_a.category_id
AND p_d2.style_name= p_a.attribute_column_value
WHERE p_d2.category_id IS NULL or p_d1.category_id IS NULL