2014-04-03 1 views
0

와 나는이 질문이있을 때 :MYSQL 그룹으로는 여러 열

SELECT `c`.*, `cont_prod`.`custom_field_set_id` AS `custom_field_set_id`, 
`cont_link_cont`.`id` AS `product_text_id`, 
`cf_321`.`value` AS `concept_number`, 
`cf_322`.`value` AS `annee`, 
`cf_323`.`value` AS `saison`, 
`cf_334`.`value` AS `color_id` 
FROM `container` AS `c` 
INNER JOIN `container_product` AS `cont_prod` 
ON c.id = cont_prod.container_id 
INNER JOIN `container_link` AS `cont_link` 
ON cont_prod.container_id = cont_link.container_id 
INNER JOIN `container` AS `cont_link_cont` 
ON cont_link_cont.id = cont_link.link_id 
LEFT JOIN `custom_field_string` AS `cf_321` 
ON cont_link_cont.id = cf_321.container_id 
AND cf_321.custom_field_id=321 
LEFT JOIN `custom_field_string` AS `cf_322` 
ON cont_link_cont.id = cf_322.container_id 
AND cf_322.custom_field_id=322 
LEFT JOIN `custom_field_string` AS `cf_323` 
ON cont_link_cont.id = cf_323.container_id 
AND cf_323.custom_field_id=323 
LEFT JOIN `custom_field_int` AS `cf_334` 
ON cont_link_cont.id = cf_334.container_id 
AND cf_334.custom_field_id=334 
WHERE `c`.`container_class` 
IN ('product') AND c.deleted = 0 
AND cont_link.reason ='product_main_text' 
AND cont_link_cont.container_class='product_text' 
AND cont_link_cont.hidden = 0 
GROUP BY CASE 
    WHEN custom_field_set_id=33 THEN cf_321.value,cf_322.value,cf_323.value,cf_334.value 
END 

을하지만 그것은 작동하지 않습니다. 일부 조건이 충족되면 기본적으로 n 개의 필드로 그룹화해야합니다. 그래서 할 것으로이 그룹과 같은 결과를 기대하고

GROUP BY CASE 
    WHEN custom_field_set_id=33 THEN cf_321.value,cf_322.value,cf_323.value,cf_334.value 
END 

편집 :

GROUP BY cf_321.value,cf_322.value,cf_323.value,cf_334.value 

EDIT2 솔루션 :이 쿼리에 잘못된 무엇

GROUP BY 
CASE WHEN custom_field_set_id=33 THEN cf_321.value END, 
CASE WHEN custom_field_set_id=33 THEN cf_322.value END, 
CASE WHEN custom_field_set_id=33 THEN cf_323.value END, 
CASE WHEN custom_field_set_id=33 THEN cf_334.value END 

여기에 쓰여진대로 Trouble with GROUP BY CASE CASE WHEN은 값 문을 1 개만 반환 할 수 있습니다.

+0

이유없이 -1 표를 얻는 사람들에게 : 저는 MySQL 전문가가 아닙니다. 나는 도움이 필요해. – albanx

답변

-2

GROUP BYCASE을 사용할 수 없습니다.

어떤 이유로 든 (*?) 어떤 레코드를 그룹화해야하고 다른 그룹을 그룹 해제해야하는 경우 UNION을 사용하십시오.

select stuff from container join other_stuff 
where 
custom_field_set_id != 33 
-- NO GROUP BY 

UNION -- or UNION ALL 

select stuff from container join other_stuff 
where 
custom_field_set_id = 33 
GROUP BY pertinent_fields 

.

(*?) GROUP BY을 사용할 수있는 기능을 사용하고 있지 않습니다.

+0

답변 해 주셔서 감사합니다. 나는이 조언을 따라 해결 : http://stackoverflow.com/questions/3490877/trouble-with-group-by-case?rq=1 – albanx

+0

기본적으로 내 문제는 내가 custom_field_set_id 값에 따라 레코드를 다르게 그룹화해야한다는 것입니다 – albanx

+4

' 당신은 GROUP BY'에서 CASE를 가질 수 없습니다. 이것은 사실이 아닙니다. 당신은 절대적으로 GROUP BY에 CASE 문을 가질 수 있습니다. – Nate