如何在MYSQL案例中使用右联接获取两列数据

时间:2019-02-14 11:03:38

标签: php mysql database join

我想获取类别定价,假设 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

2 个答案:

答案 0 :(得分:1)

您可以将max()聚合与分组依据

一起使用
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

答案 1 :(得分:1)

由于每种计费类型的pr_customer_category_pricing表中只有一个条目,因此您可以通过创建一个从pr_customer_category_pricing派生的数据透视表并为每个billing_type_id赋值来简化操作在单独的列中。然后,您可以简单地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

Demo on dbfiddle