繁体   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