2016-08-31 5 views
2

여러 행을 통해 SQL 쿼리는 다음과 같습니다 : 나는에서 올바른 PRODUCT_ID을 선택 할 수있는 SQL 쿼리를 만들 싶습니다 MySQL은, 나는 테이블이라고 clothes_properties 및 행의 일부가

id product_id property_name property 
55 189000  color   blue 
56 189000  type   tshirt 
57 189000  size   medium 
58 189001  color   red 
59 189001  type   tshirt 
60 189001  size   medium 

무엇 사용자가 검색합니다. 따라서 사용자가 tshirt 및 medium을 검색하면 SQL 쿼리가 반환됩니다. 189000 & 189001. 그리고 사용자가 tshirt 및 blue를 검색하면이 제품 ID 만 반환됩니다 (189000). SQL 쿼리는 어떻게 만듭니 까? 그거야?

+0

을보십시오. 각 특성 이름은 특성 항목에 따라 다른 필드 여야합니다. – apomene

답변

0

property_name 열을 완전히 무시 하시겠습니까?

SELECT DISTINCT product_id 
FROM clothes_properties 
WHERE property IN ('tshirt', 'medium'); 

SELECT DISTINCT product_id 
FROM clothes_properties 
WHERE property IN ('tshirt', 'blue'); 
1

이러한 종류의 테이블은 종종 키/값 저장소라고합니다. 확장 가능한 속성 목록을 처리하는 올바른 방법이지만 사용할 때 약간의 번거 로움이 될 수 있습니다.

이렇게 쿼리하면 product_id 값이 기준 목록과 일치하는 순서대로 표시됩니다. 가장 일치하는 값이 먼저옵니다. (http://sqlfiddle.com/#!9/7870b5/2/0)

select count(*) matches, product_id 
    from prop 
where property in ('tshirt','medium') 
group by product_id 
order by 1 desc 

이 조회하지만, 크기, 색상, 유형의 차이 무지.

크기, 색상 등을 정확하게 일치 시키려면 좀 더 복잡해집니다. 키/값 속성 테이블을 피벗하는 쿼리로 시작해야합니다. 행을 열로 바꿉니다. (http://sqlfiddle.com/#!9/7870b5/3/0)

select id.product_id, 
     color.property color, 
     type.property type, 
     size.property size 
from (select distinct product_id from prop) id 
left join prop color on id.product_id = color.product_id and color.property_name = 'color' 
left join prop type on id.product_id = type.product_id and type.property_name = 'type' 
left join prop size on id.product_id = size.product_id and size.property_name = 'size' 

당신은 아마도과 같이, 가상 테이블로이를 치료하고 쿼리해야합니다. (http://sqlfiddle.com/#!9/7870b5/4/0)

select * 
    from (

     select id.product_id, 
       color.property color, 
       type.property type, 
       size.property size 
     from (select distinct product_id from prop) id 
     left join prop color on id.product_id = color.product_id and color.property_name = 'color' 
     left join prop type on id.product_id = type.product_id and type.property_name = 'type' 
     left join prop size on id.product_id = size.product_id and size.property_name = 'size' 
     ) allprops 
where size='medium' and color = 'blue' 

많은 개발자와 DBA이 조금 더 쉽게 만드는 allprops 같은보기를보고 뭔가를 만들 것입니다.

0

나는 GROUP BYHAVING를 사용하여이 작업을 수행 할 것입니다 :

select product_id 
from clothes_properties 
where property_type = 'type' and property = 'tshirt' or 
     property_type = 'size' and property = 'medium' 
group by product_id 
having count(distinct property_type) = 2; 

참고하십시오 distinct가 필요하지 않은 당신이주고 제품에 대한 중복 속성 유형이없는 경우. 당신이 일반적인 뭔가를 원한다면

, 당신은 사용할 수 있습니다 :

select product_id 
from clothes_properties 
where property_type = 'type' and (property = $type or $type = '') or 
     property_type = 'size' and (property = $size or $size = '') or 
     property_type = 'color' and (property = $color or $color = '') 
group by product_id 
having count(distinct property_type) = (($type <> '') + ($size <> '') + ($color <> '')); 
+0

첫 번째 예제를 시도했지만 작은 테이블에서 테스트 할 때 완벽하게 작동합니다. 이제 더 큰 테이블에서 시도해 보겠습니다. 도와 주셔서 정말로 고맙습니다! –

0

내가 테이블이 심하게 구성되어 생각이

SELECT DISTINCT(product_id) FROM clothes_properties WHERE property IN ('tshirt','medium') group by product_id having count (distinct property) = 2