简体   繁体   English

MySQL表中的多个连接

[英]Multiple joins in MySQL table

If I have one table that references the names of sponsors and the product ids of the products they recommend, as such:如果我有一张表,其中引用了赞助商的名称和他们推荐的产品的产品 ID,如下所示:

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

And another table that lists the products:另一个列出产品的表格:

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

How do I join these together so that I have the name of sponsor plus each product name and product price that they recommend?我如何将这些连接在一起,以便我拥有赞助商的名称以及他们推荐的每个产品名称和产品价格? INNER JOIN and LEFT JOIN only seem to pull through one of the products, but not all of them. INNER JOINLEFT JOIN似乎只能通过其中一种产品,但不是所有产品。

Join twice.加入两次。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM