mysql表中的多连接

nue99wik  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(275)

如果我有一个表引用赞助商的名称和他们推荐的产品的产品ID,例如:

--------------------------------
|Name |Product ID 1|Product ID 2|
--------------------------------
|Jon  |     1      |      3     |
|Sally|     1      |      2     |
--------------------------------

以及另一个列出产品的表格:

----------------------------------------
|Product ID |Product Name|Product Price|
----------------------------------------
|     1     |  Prod 1    |    25       |
|     2     |  Prod 2    |    35       |
|     3     |  Prod 3    |    45       |
----------------------------------------

我该如何把这些联系在一起,以便我有赞助商的名字加上他们推荐的每个产品名称和产品价格? INNER JOIN 以及 LEFT JOIN 似乎只完成了其中一个产品,但不是全部。

n53p2ov0

n53p2ov01#

加入两次。

SELECT s.name, p1.ProductName AS product_1_name, p1.ProductPrice AS product_1_price, p2.ProductName AS product_2_name, p2.ProductPrice AS product_2_price
FROM sponsers AS s
JOIN products AS p1 ON s.ProductID1 = p1.ProductID
JOIN products AS p2 ON s.ProductID2 = p2.ProductID

相关问题