mysql结合了两个选择查询

时间:2013-02-18 07:21:30

标签: php mysql

我知道这可能令人困惑,但请耐心等待。

我有两个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

第二个查询在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

我对加入不太好,所以不知道是否以及如何做到这一点?

3 个答案:

答案 0 :(得分:0)

尝试以下查询,此处我在product_options上加入了两次,po.products_options_type = 6为1,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

答案 1 :(得分: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

您可以轻松排序。

答案 2 :(得分:0)

试试这个

SELECT a.products_id,a.options_values_id, b.options_values_id
FROM (SELECT @rownum:=@rownum+1 'rw', products_id, options_values_id
FROM (SELECT @rownum:=0) r, 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) a,
(SELECT @rownum:=@rownum+1 'rw', products_id,options_values_id
FROM (SELECT @rownum:=0) r, 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) b
WHERE a.products_id=b.products_id and a.rw=b.rw;

希望这有帮助。