gpt4 book ai didi

mysql - 如何比较mysql中的Varchar字段

转载 作者:行者123 更新时间:2023-11-29 18:09:02 25 4
gpt4 key购买 nike

我有以下两个表格,请看图片

表1:预定义属性 enter image description here

表2:产品详细信息 enter image description here

我正在尝试比较 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;`

这是查询结果,请看图

查询结果

enter image description here

实际上total_mismatch_count应该返回0,但是total_mismatch_count显示为2。

我已使用以下查询检查了预定义属性表中的 attribute_column_value 的 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',''));`

请帮忙。

最佳答案

我无法测试它,但类似的东西:

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

关于mysql - 如何比较mysql中的Varchar字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47549379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com