SQL 以多对多关系加入联结点 table
SQL join on junction table with many to many relation
我有 3 个 table,其中 2 个是常规数据 table,1 个是多对多结点 table。
两个数据tables:
table products
product_id | product_name | product_color
-----------------------------------------
1 | Pear | Green
2 | Apple | Red
3 | Banana | Yellow
和
table shops
shop_id | shop_location
--------------------------
1 | Foo street
2 | Bar alley
3 | Fitz lane
我有一个连接 table,其中包含 shop_id
和 product_id
:
table shops_products
shop_id | product_id
--------------------
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
3 | 2
3 | 3
我想 select 来自 shop_id 商店中的产品的数据 3. 我尝试了这里的许多示例,包括联接、左联接、内联接,但我就是不知道我在这里做什么以及出了什么问题。我有查询,但只是返回所有产品,无论它们是否在指定的商店中,如下所示:
SELECT products.product_name, products.product_color
FROM products
LEFT OUTER JOIN shops_products
ON products.product_id = shops_products.product_id
AND shops_products.shop_id = 3
LEFT OUTER JOIN shops
ON shops_products.shop_id = shops.shop_id
预期输出如下:
product_name | product_color
----------------------------
Apple | Red
Banana | Yellow
这是在MySQL,谢谢你的帮助,我真的很感激。
你可以试试这个。
SELECT products.product_name, products.product_color
FROM products
INNER JOIN shops_products
ON products.product_id = shops_products.product_id
WHERE shops_products.shop_id = 3
SELECT aa.product_id, aa.product_name, aa.product_color
FROM products AS aa
INNER JOIN shops_products AS bb
ON aa.product_id = bb.procuct_id
WHERE bb.shop_id = 3;
我喜欢从外面开始,然后搬进来。
所以想象一下所有的列都挤在一个 table 中,你可以这样写:
SELECT *
FROM products
WHERE shop_id = 3
然后您只需添加联接即可使此语句成为可能。我们知道我们接下来需要添加连接 table(因为它是直接连接到产品 table 的连接,因为它包含 product_id)。所以接下来就是加入:
SELECT products.*
FROM products
INNER JOIN shops_products
ON products.product_id = shops_products.product_id
WHERE shops_products.shop_id = 3
实际上您可以在这里停下来...因为 shop_id
已经存在于联接 table 中。但是假设您还想在最后一组列中找到商店的位置,然后您将添加 shop-table 连接。
SELECT products.*, shops.shop_location
FROM products
INNER JOIN shops_products
ON products.product_id = shops_products.product_id
INNER JOIN shops
ON shops_products.shop_id = shops.shop_id
WHERE shops_products.shop_id = 3
我有 3 个 table,其中 2 个是常规数据 table,1 个是多对多结点 table。
两个数据tables:
table products
product_id | product_name | product_color
-----------------------------------------
1 | Pear | Green
2 | Apple | Red
3 | Banana | Yellow
和
table shops
shop_id | shop_location
--------------------------
1 | Foo street
2 | Bar alley
3 | Fitz lane
我有一个连接 table,其中包含 shop_id
和 product_id
:
table shops_products
shop_id | product_id
--------------------
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
3 | 2
3 | 3
我想 select 来自 shop_id 商店中的产品的数据 3. 我尝试了这里的许多示例,包括联接、左联接、内联接,但我就是不知道我在这里做什么以及出了什么问题。我有查询,但只是返回所有产品,无论它们是否在指定的商店中,如下所示:
SELECT products.product_name, products.product_color
FROM products
LEFT OUTER JOIN shops_products
ON products.product_id = shops_products.product_id
AND shops_products.shop_id = 3
LEFT OUTER JOIN shops
ON shops_products.shop_id = shops.shop_id
预期输出如下:
product_name | product_color
----------------------------
Apple | Red
Banana | Yellow
这是在MySQL,谢谢你的帮助,我真的很感激。
你可以试试这个。
SELECT products.product_name, products.product_color
FROM products
INNER JOIN shops_products
ON products.product_id = shops_products.product_id
WHERE shops_products.shop_id = 3
SELECT aa.product_id, aa.product_name, aa.product_color
FROM products AS aa
INNER JOIN shops_products AS bb
ON aa.product_id = bb.procuct_id
WHERE bb.shop_id = 3;
我喜欢从外面开始,然后搬进来。 所以想象一下所有的列都挤在一个 table 中,你可以这样写:
SELECT *
FROM products
WHERE shop_id = 3
然后您只需添加联接即可使此语句成为可能。我们知道我们接下来需要添加连接 table(因为它是直接连接到产品 table 的连接,因为它包含 product_id)。所以接下来就是加入:
SELECT products.*
FROM products
INNER JOIN shops_products
ON products.product_id = shops_products.product_id
WHERE shops_products.shop_id = 3
实际上您可以在这里停下来...因为 shop_id
已经存在于联接 table 中。但是假设您还想在最后一组列中找到商店的位置,然后您将添加 shop-table 连接。
SELECT products.*, shops.shop_location
FROM products
INNER JOIN shops_products
ON products.product_id = shops_products.product_id
INNER JOIN shops
ON shops_products.shop_id = shops.shop_id
WHERE shops_products.shop_id = 3