簡體   English   中英

如何在MYSQL案例中使用右聯接獲取兩列數據

[英]How to get data for two columns using right join with MYSQL case

我想獲取類別定價,例如,如果customer_category_pricing表中有數據,則獲取該特定客戶的定價。 否則,從類別表中獲取默認價格。

我已經嘗試使用mysql達到預期的效果,但工作正常,但是問題是,它返回了兩行

  • hourly_amount_final列返回更新的價格,然后per_day_amount_final返回默認價格

  • 然后在下一行,hourly_amount_final列返回默認價格,然后per_day_amount_final返回更新價格。

表格:pr_categories

---------------------------------------------------------------------------------
| id | title            | hourly_amount | per_day_amount | created_at| updated_at
---------------------------------------------------------------------------------
| 1  | Power Generation | 100.00        | 200.00         | 
| 2  | Local Government | 300.00        | 400.00         |
----------------------------------------------------------

表格:pr_customer_categories_pricing

------------------------------------------------------------------------------------
| id | customer_id | category_id | billing_type_id | amount | created_at | updated_at
------------------------------------------------------------------------------------
| 1  |      1      |      1      |         1       |   109  |
| 2  |      1      |      1      |         2       |   600  |
----------------------------------------------------------

表格:pr_billing_types

----------------
| id | title   |
--------------
| 1  | Hourly  |
| 2  | Per Day |
----------------

這是我目前正在使用的查詢:

SELECT c.id,c.title,
CASE
    WHEN (c.hourly_amount <> (SELECT ccp.amount WHERE ccp.billing_type_id = 1)) 
    THEN (SELECT ccp.amount WHERE ccp.billing_type_id = 1)
    ELSE c.hourly_amount
END
AS hourly_amount_final,

CASE
    WHEN (c.per_day_amount <> (SELECT ccp.amount WHERE ccp.billing_type_id = 2)) 
    THEN  (SELECT ccp.amount WHERE ccp.billing_type_id = 2)
    ELSE c.per_day_amount
END
AS per_day_amount_final

FROM pr_customer_category_pricing AS ccp
RIGHT JOIN pr_categories AS c
ON c.id = ccp.category_id AND ccp.customer_id = 1

pr_customer_category_pricing中 沒有數據時的預期結果

----------------------------------------------------------------------
| id | title            | hourly_amount_final | per_day_amount_final |
----------------------------------------------------------------------
| 1  | Power Generation |      100.00         |      200.00          | 
| 2  | Local Government |      300.00         |      600.00          |
----------------------------------------------------------------------

pr_customer_category_pricing中 存在數據時的預期結果

----------------------------------------------------------------------
| id | title            | hourly_amount_final | per_day_amount_final |
----------------------------------------------------------------------
| 1  | Power Generation |      109.00         |      600.00          | 
| 2  | Local Government |      300.00         |      400.00          |
----------------------------------------------------------------------

我得到的實際結果是:

----------------------------------------------------------------------
| id | title            | hourly_amount_final | per_day_amount_final |
----------------------------------------------------------------------
| 1  | Power Generation |      109.00         |      200.00          | 
| 1  | Power Generation |      100.00         |      600.00          |
| 2  | Local Government |      300.00         |      400.00          |
----------------------------------------------------------------------

我究竟做錯了什么? 有什么建議么! 幫助一個兄弟。 :S

您可以將max()聚合與group by一起使用

SELECT c.id,c.title,
max(CASE
    WHEN (c.hourly_amount <> (SELECT ccp.amount WHERE ccp.billing_type_id = 1)) 
    THEN (SELECT ccp.amount WHERE ccp.billing_type_id = 1)
    ELSE c.hourly_amount
END)
AS hourly_amount_final,

max(CASE
    WHEN (c.per_day_amount <> (SELECT ccp.amount WHERE ccp.billing_type_id = 2)) 
    THEN  (SELECT ccp.amount WHERE ccp.billing_type_id = 2)
    ELSE c.per_day_amount
END)
AS per_day_amount_final

FROM pr_customer_category_pricing AS ccp
RIGHT JOIN pr_categories AS c
ON c.id = ccp.category_id AND ccp.customer_id = 1
group by c.id,c.title

由於每種計費類型在pr_customer_category_pricing表中只能有一個條目,因此可以通過從pr_customer_category_pricing創建派生的數據透視表billing_type_id在單獨的列中為每個pr_customer_category_pricing的值來簡化操作,從而簡化操作。 然后,您可以簡單地COALESCE從從價值導出表中的值pr_categories每個billing_type_id

SELECT c.id,c.title,
       COALESCE(ccp.hourly_amount, c.hourly_amount) AS hourly_amount_final,
       COALESCE(ccp.per_day_amount, c.per_day_amount) AS per_day_amount_final
FROM (SELECT
          customer_id,
          category_id,
          MAX(CASE WHEN billing_type_id = 1 THEN amount END) AS hourly_amount,
          MAX(CASE WHEN billing_type_id = 2 THEN amount END) AS per_day_amount
     FROM pr_customer_category_pricing
     GROUP BY customer_id, category_id) AS ccp
RIGHT JOIN pr_categories AS c
ON c.id = ccp.category_id AND ccp.customer_id = 1

輸出:

id  title               hourly_amount_final per_day_amount_final
1   Power Generation    109                 600
2   Local Government    300                 400

dbfiddle上的演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM