MySQL多重连接与多对多关系

时间:2014-10-30 09:01:10

标签: mysql sql relational-database

我有三个对我很重要的表:customer,client_assignment和customer_products。最后两个是多对多关系的赋值表。

在client_assignment中,client类型的客户与另一位客户关联(其中customer_id是父项,client_id是该项客户。) 在customer_product中,我将客户与产品联系起来。

客户无法与产品关联,他从父母客户那里继承了产品。在下面的例子中,这意味着, Foo-1 也有产品3,因为他的父亲( Foo )拥有它。

customer (Customers):
+-------------+-------+----------+
| customer_id | name  | type     |
+-------------+-------+----------+
|           1 | Foo   | customer |
|           2 | Foo-1 | client   |
|           3 | Foo-2 | client   |
|           4 | Bar   | customer |
|           5 | Foob  | customer |
+-------------+-------+----------+

client_assignment (Customer/Client-Assignment):
+-------------+-----------+
| customer_id | client_id |
+-------------+-----------+
|           1 |         2 |
|           1 |         3 |
+-------------+-----------+

customer_product (Customer/Product-Assignment):
+-------------+------------+
| customer_id | product_id |
+-------------+------------+
|           1 |          3 |
|           1 |          4 |
|           1 |          5 |
|           4 |          3 |
|           5 |          7 |
+-------------+------------+

我想要完成以下操作:选择与产品X关联的所有客户各自的客户。

我对产品3的期望结果是这样的:

+-------------+-------+--------+
| customer_id | name  | parent |
+-------------+-------+--------+
|           1 | Foo   | null   |
|           2 | Foo-1 | 1      |
|           3 | Foo-2 | 1      |
|           4 | Bar   | null   |
+-------------+-------+--------+

我一直在考虑这个问题,看起来相当复杂。我试过加入他们,如下所示:

SELECT c2.customer_id, c2.name, c1.customer_id as parent
  FROM  customer_product p, customer c1, customer c2, client_assignment a
 WHERE 
           c1.customer_id = p.customer_id 
       AND c2.customer_id = a.client_id 
       AND a.customer_id = c1.customer_id 
       AND p.product_id = 3

我知道,这个查询不会给我完全想要的结果,但是我已经创建了它。它的主要问题是,它只选择客户端,而不是客户自己。因此,我只得到 Foo-1 Foo-2 ,但不是 Bar Foo 。< / p>

问题是:这是否可以轻易实现,以及如何实现?

2 个答案:

答案 0 :(得分:2)

您可以编写另一个SELECT来获取客户,并将两者合并到UNION

SELECT c.customer_id, c.name, NULL AS parent
  FROM customer AS c
  JOIN customer_product AS p ON c.customer_id = p.customer_id
 WHERE c.type = 'customer' 
  AND p.product_id = 3

UNION

SELECT c2.customer_id, c2.name, c1.customer_id AS parent
 FROM customer_product AS p
 JOIN customer AS c1 ON c1.customer_id = p.customer_id
 JOIN client_assignment AS a ON a.customer_id = c1.customer_id
 JOIN customer AS c2 ON c2.customer_id = a.client_id
WHERE c2.type = 'client'
  AND p.product_id = 3

答案 1 :(得分:0)

因为您必须获取客户端和客户父母的名称,所以使用UNION的方法是这样做。

with customer_names as (select customer_id from s_customer_product where product_id =3),

client_names as (select client_id , customer_id as Parent_id from s_client_assignment join customer_names using(customer_id))

select customer_id , name , null as Parent from  s_customers join customer_names using(customer_id) 

union 

select a.client_id , b.name , a.parent_id as Parent from client_names a, s_customers b where b.customer_id = a.client_id;