连接2个表以使用某些情况带来多个列

时间:2016-12-28 04:55:09

标签: mysql sql

我有一张表table1

 pid_x | pid_y | count
-------|-------|-------
 a     | b     |  5
 a     | c     | 10
 b     | a     | 20
 c     | e     |  3
 d     | g     |  7
 e     | f     | 10
 e     | b     | 20

依此类推(超过100万行)

我还有master_table  其中包含细节

 master_id | pid | rating_pid | price_pid
-----------|-----|------------|-----------
 a         | a1  |          2 | 10
 a         | b1  |          4 | 20
 a         | c1  |          1 | 30
 b         | d1  |        3.5 | 40
 b         | e1  |        2.4 | 50
 c         | f1  |        1.5 | 60
 d         | g1  |        3.8 | 70

(等等,巨大的表master_table

你可以在table1中看到,pid_x和pid_y有一些实际上是master_id的值(来自master_table),我想用pid_x和pid_y master_id的最便宜'pid'替换master_id >

我要找的是final_table 其中有列

pid_x pid_y count price_pid_y rating_pid_y price_diff_pidy/x

解释

使用master_table中最便宜的pid替换master_id值后,创建一个包含pid_xpid_y列的最终表,然后从price and rating {{1}加入pid_y的master_table's还有pid_y相对于pid_x的价格差异百分比。

pid应该看起来像这样

final_table

我有一个部分解决方案,它给了我结果,

我只想添加这个条件“只要在table1中pid_x和pid_y有来自master_id的值,用master_table中最便宜的pid替换它。”

 pid_x | pid_y | count | price_pid_y | rating_pid_y | price_diff_pid_y/x
-------|-------|-------|-------------|--------------|--------------------
 a1    | d1    |     5 |          40 |          3.5 |              400
 a1    | f1    |    10 |          60 |          1.5 |              600
 d1    | a1    |    20 |          10 |            2 |               25

给出一些像

这样的案例
SELECT
  t1.pid_x
  ,t1.pid_y
  ,t1.count
  ,m1.price_pid AS price_pid_y
  ,m1.rating_pid AS rating_pid_y
  ,100 * m1.price_pid / m2.price_pid AS price_diff_pid_y_x
FROM table1 t1
    INNER JOIN master_table m1 ON t1.pid_y = m1.pid
    INNER JOIN master_table m2 ON t1.pid_x = m2.pid
ORDER BY
  t1.pid_x
  ,t1.pid_y

然后用最便宜的pid dor替换那个pid_x mater_id ......`

1 个答案:

答案 0 :(得分:0)

好吧,我想我明白了,所以我将逐步采取这一步骤,这样你就可以学习如何做到这一点并修复我不理解的地方。

首先,您需要最便宜的价格"当你在主表上查看东​​西时。让我们进行查询以获得该表。

首先,我们获取该行的唯一标识符:

 SELECT master_id, min(price_pid) as price_pid
 FROM master_table
 GROUP BY master_id

然后我们可以选择只有一个最便宜的新表:

SELECT ma.* 
FROM master_table ma
JOIN (
  SELECT master_id, min(price_pid) as price_pid
  FROM master_table
  GROUP BY master_id
) as mi_ma on ma.master_id = mi_ma.master_id and ma.price_pid = mi_ma.price_pid

我现在将其创建为这样的视图:

CREATE OR REPLACE VIEW min_master_table AS
  SELECT ma.* 
  FROM master_table ma
  JOIN (
    SELECT master_id, min(price_pid) as price_pid
    FROM master_table
    GROUP BY master_id
  ) as mi_ma on ma.master_id = mi_ma.master_id and ma.price_pid = mi_ma.price_pid

使用这个新视图,您的查询非常接近,此处已修复:

SELECT
  m1.pid as pid_x,
  m2.pic as pid_y,
  t1.count,
  m2.price_pid AS price_pid_y,
  m2.rating_pid AS rating_pid_y,
  100 * m2.price_pid / m1.price_pid AS price_diff_pid_y_x
FROM table1 t1
JOIN min_master_table m1 ON t1.pid_x = m1.pid
JOIN min_master_table m2 ON t1.pid_y = m2.pid
  

注意:如果您无法进行查看,这也会有效,但由于代码重复,它更容易出错:

SELECT
  m1.pid as pid_x,
  m2.pic as pid_y,
  t1.count,
  m2.price_pid AS price_pid_y,
  m2.rating_pid AS rating_pid_y,
  100 * m2.price_pid / m1.price_pid AS price_diff_pid_y_x
FROM table1 t1
JOIN (
  SELECT ma.* 
  FROM master_table ma
  JOIN (
    SELECT master_id, min(price_pid) as price_pid
    FROM master_table
    GROUP BY master_id
  ) as mi_ma on ma.master_id = mi_ma.master_id and ma.price_pid = mi_ma.price_pid
) m1 ON t1.pid_x = m1.pid
JOIN (
  SELECT ma.* 
  FROM master_table ma
  JOIN (
    SELECT master_id, min(price_pid) as price_pid
    FROM master_table
    GROUP BY master_id
  ) as mi_ma on ma.master_id = mi_ma.master_id and ma.price_pid = mi_ma.price_pid
) m2 ON t1.pid_y = m2.pid