MySQL - 加入或插入查询结果

时间:2014-08-08 15:16:53

标签: jquery mysql database insert jointable

我希望能够将两个查询结果合并到一个单表或单个查询结果中。我正在结合使用woocommerce的表格。

第一次查询结果包含销售订单和送货详情:

SELECT bso.ID AS order_item_id,bsd.NAME AS order_item_name,' shipping' AS order_item_type,bso.ID AS order_id 从bso LEFT OUTER JOIN bsd ON bso.DELIVERY_ID = bsd.ID 在哪里bso.ID IN(255,810)

第二个查询包含 order_id 作为链接的订购商品:

SELECT bsb.ID AS order_item_id,bsb.NAME AS order_item_name,' line_item' AS order_item_type,bsb.ORDER_ID AS order_id 来自b_sale_basket bsb WHERE bsb.ORDER_ID IN(810,255)

http://i.stack.imgur.com/auFXa.png

我必须将两个查询结果合并,并且应该如下所示:

http://i.stack.imgur.com/2Td6o.png

请指导我如何做到这一点。感谢任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

如果必须合并两个结果,可以尝试使用UNION

http://dev.mysql.com/doc/refman/5.0/en/union.html

您的查询最终会看起来像:

SELECT * FROM (
SELECT bso.ID AS order_item_id, bsd.NAME AS order_item_name, 'shipping' AS order_item_type, bso.ID AS order_id FROM bso LEFT OUTER JOIN bsd ON bso.DELIVERY_ID = bsd.ID WHERE bso.ID IN (255, 810)
UNION
SELECT bsb.ID AS order_item_id, bsb.NAME AS order_item_name, 'line_item' AS order_item_type, bsb.ORDER_ID AS order_id FROM b_sale_basket bsb WHERE bsb.ORDER_ID IN (810, 255)
) aux ORDER BY order_id;