2013-11-23 2 views
0

빠른 방법을 찾고 있습니다/실제로 해당 제품에 대한 할인이있는 경우 보조 테이블에서 할인 된 가격을 사용하는 것을 의미합니다. 그렇지 않으면 첫 번째 테이블 가격.조건부로 보조 테이블에서 다른 값을 사용하십시오. 그렇지 않으면 첫 번째 테이블 값을 사용하십시오.

#########로 표시된 부분은 내가 갇혀있는 부분입니다 ... 무엇을해야합니까?

현재 내가 가지고있는 것은 이것이다 :

SELECT 
cat.`CategoryId` AS CategoryId 
,prod.`ProductId` AS ProductId 
,COUNT(prod.`ProductId`)AS product_count 
,prod.`Price` AS price 
,adj.`ProductPrice` AS Adj_Price 

FROM ax_category cat 
INNER JOIN xx_products prod USING(categoryid) 
INNER JOIN xx_subcategory sc ON(prod.Subcategoryid = sc.SubCategoryId) 
INNER JOIN xx_services s USING(productid) 
LEFT JOIN xx_invoiceline il USING(serviceid) 
LEFT JOIN xx_invoices i USING(invoiceid) 
#LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`) 
LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`) 
WHERE DATE_FORMAT(s.DateEnd,'%Y-%m') = '2013-11' 

############################################ 
IF EXISTS(
SELECT a.`ProductPrice` AS price 
FROM xx_adjustments AS a 
WHERE a.`ProductId` = '999' 
#limit 1 
) 
############################################ 


GROUP BY prod.`ProductId` 

답변

0

당신은 COALESCE()을 사용할 수

SELECT 
cat.`CategoryId` AS CategoryId 
,prod.`ProductId` AS ProductId 
,COUNT(prod.`ProductId`)AS product_count 
,prod.`Price` AS price 
,adj.`ProductPrice` AS Adj_Price 
,COALESCE(adj.`ProductPrice`, prod.`Price`) AS discountedPrice 
FROM ax_category cat 
INNER JOIN xx_products prod USING(categoryid) 
INNER JOIN xx_subcategory sc ON(prod.Subcategoryid = sc.SubCategoryId) 
INNER JOIN xx_services s USING(productid) 
LEFT JOIN xx_invoiceline il USING(serviceid) 
LEFT JOIN xx_invoices i USING(invoiceid) 
#LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`) 
LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`) 
WHERE DATE_FORMAT(s.DateEnd,'%Y-%m') = '2013-11' 
GROUP BY prod.`ProductId` 

간단한 예제 here를 참조하십시오.

+0

@ user2917229를 정렬 해 주셔서 감사합니다. 하나님의 축복과 해피 코딩! – Edper

0

이것에 대해 (SELECT 절에 discountedPrice 열 참조) 방법 :

SELECT 
cat.`CategoryId` AS CategoryId 
,prod.`ProductId` AS ProductId 
,COUNT(prod.`ProductId`)AS product_count 
,prod.`Price` AS price 
,adj.`ProductPrice` AS Adj_Price 
,IFNULL(adj.`ProductPrice`, prod.`Price`) AS discountedPrice 

FROM ax_category cat 
INNER JOIN xx_products prod USING(categoryid) 
INNER JOIN xx_subcategory sc ON(prod.Subcategoryid = sc.SubCategoryId) 
INNER JOIN xx_services s USING(productid) 
LEFT JOIN xx_invoiceline il USING(serviceid) 
LEFT JOIN xx_invoices i USING(invoiceid) 
#LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`) 
LEFT JOIN xx_adjustments adj ON (prod.`ProductId` = adj.`ProductId` AND prod.`Quantity` = adj.`Quantity`) 
WHERE DATE_FORMAT(s.DateEnd,'%Y-%m') = '2013-11' 

GROUP BY prod.`ProductId` 

특히 productId에, 사용 xx_adjustment에는 제품 가격이없는 경우 xx_products.Price의 가격입니다.

+0

이 줄은'LEFT JOIN xx_adjustments adj ON (s.ProductId = adj.ProductId 및 s.Quantity = adj.Quantity)'입니다. 그렇지 않아야한다 : LEFT JOIN xx_adjustments adj ON (prod.ProductId = adj.ProductId AND prod.Quantity = adj.Quantity)'? – har07

+0

안녕하세요 @ user2917229, 방금 내 대답이 업데이트되었습니다. 내 쿼리를 다시 실행하면 어떻게됩니까? – har07

+0

이고 Adj_Price에 대해 반환 된 값은 null입니까? – har07

관련 문제