0

これが混乱を招く可能性があることは承知していますが、これについてはご容赦ください。

ほとんど同じ結果セットを返すわずかな違いがある 2 つの SELECT クエリがあります。

SELECT products_id,options_values_id
    FROM products_attributes pa
    LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
    WHERE products_id ='574' and pa.options_id!=6
    and pa.options_id!=3
    AND products_options_type = 6
    GROUP BY products_id,options_values_id
    ORDER BY products_id,products_options_sort_order,options_id

2 番目のクエリは次の場所で異なりますproducts_options_type

SELECT products_id,options_values_id
    FROM products_attributes pa
    LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
    WHERE products_id ='574' and pa.options_id!=6
    and pa.options_id!=3
    AND products_options_type = 2
    GROUP BY products_id,options_values_id
    ORDER BY products_id,products_options_sort_order,options_id

そして、それらによって返される結果は

574|193
574|204

574|25
574|3

次のように出力したい

574|193|25
574|204|3

私が試したのは:

SELECT pa.products_id,pa.options_values_id,ord.options_values_id
        FROM products_attributes pa
        LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )


        LEFT JOIN(SELECT products_id,options_values_id
        FROM products_attributes pa
        LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
        WHERE products_id ='574' and pa.options_id!=6
        and pa.options_id!=3
        AND products_options_type = 2
        GROUP BY products_id,options_values_id
        ORDER BY products_id,products_options_sort_order,options_id)ord ON pa.products_id=ord.products_id


        WHERE paproducts_id ='574' and pa.options_id!=6
        and pa.options_id!=3
        AND products_options_type = 2
        GROUP BY pa.products_id,pa.options_values_id
        ORDER BY pa.products_id,products_options_sort_order,options_id

ただし、これは戻ります

574|193|25
574|204|25

私は結合があまり得意ではないので、これを行うことができるかどうか、またどのように行うことができるか考えていますか?

4

3 に答える 3

0

以下のクエリを試してください。ここではproduct_options、1 回po.products_options_type = 6目と 2 回目の 2回参加しました。po1.products_options_type = 2

SELECT pa.products_id,po.options_values_id,po1.options_values_id
FROM products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id and po.products_options_type = 6)
LEFT JOIN products_options po1 ON ( pa.options_id = po1.products_options_id and po.products_options_type = 2)
WHERE pa.products_id ='574' and pa.options_id!=6
and pa.options_id!=3
GROUP BY products_id,po1.options_values_id,po1.options_values_id
ORDER BY products_id,products_options_sort_order,options_id
于 2013-02-18T07:27:06.247 に答える
0

代替ソリューション:

select v1,v2,v3, v4 from (SELECT products_id v1,options_values_id v2
    FROM products_attributes pa
    LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
    WHERE products_id ='574' and pa.options_id!=6
    and pa.options_id!=3
    AND products_options_type = 6
    GROUP BY products_id,options_values_id
    ORDER BY products_id,products_options_sort_order,options_id) t1,    
(SELECT products_id v3,options_values_i v4
    FROM products_attributes pa
    LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
    WHERE products_id ='574' and pa.options_id!=6
    and pa.options_id!=3
    AND products_options_type = 2
    GROUP BY products_id,options_values_id
    ORDER BY products_id,products_options_sort_order,options_id) t2
where t1.products_id=t2.products_id

簡単に並べ替えることができます。

于 2013-02-18T07:27:24.063 に答える