2017-11-29 16 views
0

에 대 Varchar 필드를 비교하는 방법은 다음과 같은 두 개의 테이블을 가지고, 이미지mysql을

표를 참조하십시오 predefined_attributes enter image description here

표 2에게 : product_detail 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;` 

이 쿼리 결과, 실제로 total_mismatch_count 0을 반환 갈까요

enter image description here

이미지

쿼리 결과를 참조하십시오하지만 total_mismatch_count로 (2)를 보여줍니다.

아래 쿼리를 사용하여 color_name 및 style_name에 대한 predefined_attributes 테이블에서 attribute_column_value를 확인했는데 출력과 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');` 

는 MySQL의 쿼리가이 경우 올바른 결과 인 0으로 total_mismatch_count 반환하도록 VARCHAR를 비교하는 가장 좋은 방법은 무엇입니까?, 내가 뭔가 잘못하고 무엇입니까?

참고 :이 같은 또한 내가 업데이트 한 모두 테이블이 공백

`update predefined_attributes set attribute_column_value= 
    trim(replace(replace(replace(attribute_column_value,'\t',''),'\n',''),'\r',''));` 

도와주세요

을하지 않도록.

+0

당신은 무엇 출력을 원하는가? –

+0

두 열 모두로 트림을 사용해보십시오. (색상 _ 이름)이 아닌 경우 (색상 _ 속성 _ 트리밍 _ 선택 _ 속성 _ 컬럼 _ 선택 _ 속성 _ 컬럼 _을 _ 선택 _합니다. 여기서 category_id = 1 및 속성 _ 컬럼 _ 이름 = '색상 _ 이름') 1 else 0 END –

+0

@JeanDoux 두 테이블이 동일한 값을 가지면 출력은 0이어야합니다. – davidb

답변

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 
+0

나는 당신의 쿼리를 시도했으나 기대했던 결과도 얻지 못하고있다. – davidb