如何在SQL或PHP中组合这两个sql查询的结果..它们都涉及连接..我想将它们组合起来并按orderid排序..我该怎么办?< / p>
第一次查询
$sqlstr = "SELECT op.* FROM products_to_products_extra_fields AS p
INNER JOIN orders_roster AS r ON p.products_id = r.products_id
INNER JOIN orders_products AS op ON r.orders_id = op.orders_id
INNER JOIN orders AS o on op.orders_id = o.orders_id
WHERE p.products_extra_fields_id = 14
AND p.products_extra_fields_value between '"
. tep_db_input($startdate) . "' and '"
. tep_db_input($enddate) . " 23:59:59'
AND r.roster_status != 'Removed'
AND o.payment_method = 'Institutional Billing'
AND o.orders_status < 100001
GROUP BY o.orders_id
ORDER BY DECODE(o.cc_type, '$salt') ASC";
第二次查询
$sqlstr2 = "SELECT op.* FROM products_to_products_extra_fields AS p
INNER JOIN orders_products AS op ON p.products_id = op.products_id
INNER JOIN orders AS o on op.orders_id = o.orders_id
WHERE p.products_id IN
(SELECT products_id
FROM products_to_products_extra_fields
WHERE p.products_id NOT IN
(SELECT products_id
FROM products_to_products_extra_fields
WHERE products_extra_fields_id = 14)
)
AND o.date_purchased between '"
. tep_db_input($startdate) . "' and '"
. tep_db_input($enddate) . " 23:59:59'
AND o.payment_method = 'Institutional Billing'
AND o.orders_status < 100001
GROUP BY o.orders_id
ORDER BY DECODE(o.cc_type, '$salt') ASC";
答案 0 :(得分:2)
如果你需要将它们组合在PHP端,我会假设你留下了一组数组(MySQL Rows),你可以简单地遍历两组结果并使用array_push将它们推入第三(完整的)数组。您也可以使用array_merge,但有时使用多维数组,最终结果不是您所期望的。
http://us3.php.net/manual/en/function.array-push.php http://us3.php.net/function.array-merge
答案 1 :(得分:2)
只需进行UNION查询并在SQL端合并结果。不需要PHP。