如何在同一个表上进行多个连接,然后与另一个表连接?

时间:2015-10-15 06:23:19

标签: mysql sql-server join

我想在同一张桌子上进行内部联接,product_attributes我需要prod_value = genderprod_attr=Male以及maingroup = Pants。这意味着,我希望所有性别为男性的产品都属于Pants。我甚至想要打印prodgroup并加入product_master表,我需要prod_name。我怎样才能做到这一点?

表1:Product_attributes

+----+------------+-----------+------------+
| id | prod_style | prod_attr | prod_value |
+----+------------+-----------+------------+
|  1 | 0010       | gender    |       Male |
|  2 | 0010       | maingroup |      Pants |
|  3 | 0010       | prodgroup |  Pants_Abc |
|  4 | 0010       | Blue      |      color |
|  5 | 0011       | gender    |       Male |
|  6 | 0011       | maingroup |      Pants |
|  7 | 0011       | prodgroup |  Pants_Pqr |
|  8 | 0012       | gender    |     Female |
|  9 | 0012       | maingroup |      Pants |
| 10 | 0012       | prodgroup |      Pants |
| 11 | 0013       | gender    |     Female |
| 12 | 0013       | maingroup |    Jackets |
+----+------------+-----------+------------+

表2:Product_master

+----+------------+-----------+
| id | prod_style | prod_name | 
+----+------------+-----------+
|  1 | 0010       | ABC       |
|  2 | 0011       | PQR       |
|  3 | 0012       | XYZ       |
|  4 | 0013       | LMN       |
+----+------------+-----------+

我尝试过这个解决方案:

select 
    * 
from 
    product_master pm 
    INNER JOIN (select 
                    * 
                from 
                    product_attributes 
                where 
                    prod_value='prodgroup' 
                        and prod_style in(select 
                                                prod_style 
                                          from 
                                                product_attributes 
                                           where 
                                                prod_attr ='pants' 
                                                and prod_value='mainGroup' 
                                                and prod_style in(select 
                                                                        prod_style 
                                                                   from 
                                                                        product_attributes 
                                                                   where 
                                                                        prod_attr='Male'
                                                                  )
                                           )
                ) p ON pm.prod_style = p.prod_style  
ORDER By 
    prod_name

使用我的解决方案,我得到了输出,但不知道它是否是正确的查询方式。

使用上述解决方案输出:

+-----------+------------+-----------+-------------+-----------+-----------+
| id        | prod_style | prod_name | prod_style  | prod_attr | prod_value|
+-----------+------------+-----------+-------------+-----------+-----------+
|  1        | 0010       | ABC       |0010         |Pants_Abc  |prodgroup  |
|  2        | 0011       | PQR       |0011         |Pants_Pqr  |prodgroup  |
|  3        | 0012       | XYZ       |0012         |Pants      |prodgroup  |
|  4        | 0013       | LMN       |0013         |skinny     |prodgroup  |
+-----------+------------+-----------+-------------------------------------+

3 个答案:

答案 0 :(得分:0)

试试这个..

SELECT  *
FROM    product_master pm
        JOIN ( SELECT   t1.*
               FROM     product_attributes t1
                        JOIN product_attributes t2 ON t1.prod_style = t2.prod_style
                                                      AND t2.prod_attr = 'gender'
                                                      AND t2.prod_value = 'male'
                        JOIN product_attributes t3 ON t1.prod_style = t3.prod_style
                                                      AND t3.prod_attr = 'maingroup'
                                                      AND t3.prod_value = 'Pants'
             ) pa ON pm.prod_style = pa.prod_style

答案 1 :(得分:0)

试试这个,它将每个其他 attr放在自己的行上:

select pm.prod_style, pm.prod_name, pa.prod_attr, pa.prod_value
from Product_attributes pa
join Product_attributes pa_m on pa.prod_style = pa_m.prod_style
    and pa_m.prod_attr = 'gender' and pa_m.prod_value = 'Male'
join Product_attributes pa_g on pa.prod_style = pa_m.prod_style
    and pa_g.prod_attr = 'maingroup' and pa_g.prod_value = 'Pants'
join Product_master pm on pm.prod_style = pa.prod_style
where pa.prod_attr not in ('gender', 'maingroup')

如果您想为每个产品添加一行,请尝试以下操作:

select pm.prod_style, pm.prod_name, group_concat(concat(pa.prod_attr, '=', pa.prod_value)) attrs
from Product_attributes pa
join Product_attributes pa_m on pa.prod_style = pa_m.prod_style
    and pa_m.prod_attr = 'gender' and pa_m.prod_value = 'Male'
join Product_attributes pa_g on pa.prod_style = pa_m.prod_style
    and pa_g.prod_attr = 'maingroup' and pa_g.prod_value = 'Pants'
join Product_master pm on pm.prod_style = pa.prod_style
where pa.prod_attr not in ('gender', 'maingroup')
group by 1, 2

将产生类似

的输出
prod_style | prod_name | attrs
0010       | ABC       | color=Blue,size=Large

答案 2 :(得分:0)

我已经构建了这个查询:

DECLARE @Product_attributes TABLE
(
    id INT
    , prod_style VARCHAR(10)
    , prod_attr VARCHAR(10)
    , prod_value VARCHAR(10)
);

DECLARE @Product_master TABLE
(
    ID INT
    , prod_style VARCHAR(10)
    , prod_name VARCHAR(10)
);

INSERT INTO @Product_attributes (id, prod_style, prod_attr, prod_value)
VALUES (1, '0010', 'gender','Male')
    , (2, '0010', 'maingroup','Pants')
    , (3, '0010', 'prodgroup', 'Pants_Abc')
    , (4, '0010', 'Blue', 'color')
    , (5, '0011', 'gender', 'Male')
    , (6, '0011', 'maingroup', 'Pants')
    , (7, '0011', 'prodgroup', 'Pants_Pqr')
    , (8, '0012', 'gender', 'Female')
    , (9, '0012', 'maingroup', 'Pants')
    , (10, '0012', 'prodgroup', 'Pants')
    , (11, '0013', 'gender', 'Female')
    , (12, '0013', 'maingroup', 'Jackets');

INSERT INTO @Product_master (id, prod_style, prod_name)
VALUES (1, '0010', 'ABC')
    , (2, '0011', 'PQR')
    , (3, '0012', 'XYZ')
    , (4, '0013', 'LMN');

SELECT PA1.id, PA1.prod_style, PM.prod_name, PA1.prod_attr, PA1.prod_value
FROM @Product_attributes AS PA1
INNER JOIN @Product_master AS PM
    ON PM.prod_style = PA1.prod_style
WHERE PA1.prod_attr = 'mainGroup'
    AND PA1.prod_value = 'Pants'
    AND EXISTS (
        SELECT 1
        FROM @Product_attributes AS PA2
        WHERE PA2.prod_style = PA1.prod_style
            AND PA2.prod_attr = 'gender'
            AND PA2.prod_value = 'Male'
        );

检查您的产品(基于prod_style)是否适用于男性并且是裤子。必须满足两个条件,但它的输出与您的不同,即:

╔════╦════════════╦═══════════╦═══════════╦════════════╗
║ id ║ prod_style ║ prod_name ║ prod_attr ║ prod_value ║
╠════╬════════════╬═══════════╬═══════════╬════════════╣
║  2 ║       0010 ║ ABC       ║ maingroup ║ Pants      ║
║  6 ║       0011 ║ PQR       ║ maingroup ║ Pants      ║
╚════╩════════════╩═══════════╩═══════════╩════════════╝

我的逻辑中有什么损失吗?请告诉我。