Php mysql结合了两个select查询
我知道这可能会让人困惑,但请在这一点上容忍我 我有两个SELECT查询,它们之间的差别很小,返回的结果集几乎相同Php mysql结合了两个select查询,php,mysql,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_
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\u options\u 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|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
我对连接不太在行,所以你知道这是否以及如何做到吗?试试下面的查询,这里我对
product\u options
进行了两次连接,1次为po.products\u options\u type=6
,2次为po1.products\u options\u 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
替代解决方案:
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
您可以轻松地对其进行排序。试试这个
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;
希望这能有所帮助。为什么您的数据库引擎应该知道193和25与204和3一样属于一个整体?看到你的代码,我就不知道了。实际上里面没有太多的逻辑,但我正在处理一个现有的系统,我需要这部分的快捷方式(只是不要问为什么我需要在这里作弊)。我的观点不是你没有理由这样需要你的数据。只是引擎无法在不知道如何排序的情况下对数据进行排序。
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;