我有这个select语句,它运行一个标题为' product_id'和一个价值。
SELECT meta_value as product_id
FROM ctc_woocommerce_order_itemmeta
LEFT JOIN ctc_woocommerce_order_items ON ctc_woocommerce_order_itemmeta.order_item_id = ctc_woocommerce_order_items.order_item_id
WHERE meta_key = '_product_id'
AND order_id = 3988
我希望结果与此声明相结合:
SELECT meta_value as variation_id
FROM ctc_woocommerce_order_itemmeta
LEFT JOIN ctc_woocommerce_order_items ON ctc_woocommerce_order_itemmeta.order_item_id = ctc_woocommerce_order_items.order_item_id
WHERE meta_key = '_variation_id'
AND order_id = 3988
如何结合2?我试过UNION,也试过类似的声明,但是phpMyAdmin崩溃了。
SELECT x.a, y.b FROM (SELECT * from a) as x, (SELECT * FROM b) as y
我正在寻找带有2列的输出:
1 x product_id
1 x variation_id
答案 0 :(得分:1)
我认为你在错误的地方写了SELECT。
SELECT (
SELECT meta_value as product_id
FROM ctc_woocommerce_order_itemmeta
LEFT JOIN ctc_woocommerce_order_items ON ctc_woocommerce_order_itemmeta.order_item_id = ctc_woocommerce_order_items.order_item_id
WHERE meta_key = '_product_id'
AND order_id = 3988
) as product_id,
(
SELECT meta_value as variation_id
FROM ctc_woocommerce_order_itemmeta
LEFT JOIN ctc_woocommerce_order_items ON ctc_woocommerce_order_itemmeta.order_item_id = ctc_woocommerce_order_items.order_item_id
WHERE meta_key = '_variation_id'
AND order_id = 3988
) as variation_id;
答案 1 :(得分:0)
尝试使用子查询加入:
SELECT t1.meta_value as product_id, sub1.variation_id AS variation_id
FROM ctc_woocommerce_order_itemmeta t1
LEFT JOIN ctc_woocommerce_order_items ON
ctc_woocommerce_order_itemmeta.order_item_id =
ctc_woocommerce_order_items.order_item_id
JOIN
(SELECT meta_key, order_id, meta_value as variation_id
FROM ctc_woocommerce_order_itemmeta
LEFT JOIN ctc_woocommerce_order_items ON
ctc_woocommerce_order_itemmeta.order_item_id =
ctc_woocommerce_order_items.order_item_id
) AS sub1
WHERE t1.meta_key = '_product_id' AND t1.order_id = 3988
AND sub1.meta_key = '_variation_id' AND sub1.order_id = 3988
答案 2 :(得分:0)
我尝试将子查询中的两个id字段排序,然后将其连接到订单行表。
另外,当你在一个表中的一个字段上添加一个WHERE条件时,它就是OUTER JOINing,它就变成了一个INNER JOIN,无论如何我觉得这真的是你想要的。为了便于阅读,我添加了表别名。
可能有一种更优雅的方式从该子查询中获取id字段,但是如果没有样本数据,我使用的方法我非常肯定会起作用。但是,自联接可能会更高效。
SELECT
im.product_id,
im.variation_id
FROM
(
SELECT
order_item_id,
MAX(CASE WHEN meta_key = '_product_id' THEN meta_value ELSE '' END) AS product_id,
MAX(CASE WHEN meta_key = '_variation_id' THEN meta_value ELSE '' END) AS variation_id
FROM
ctc_woocommerce_order_itemmeta
) AS im
INNER JOIN
ctc_woocommerce_order_items AS oi
ON im.order_item_id = oi.order_item_id
AND
oi.order_id = 3988
答案 3 :(得分:0)
在我看来,您甚至不需要LEFT JOIN
和ctc_woocommerce_order_items
表。
由于ctc_woocommerce_order_items
表中不需要任何字段。同样使用table_A LEFT JOIN table_B
,这意味着无论table_B
中是否存在匹配记录,始终从table_A
返回记录。所以我在这里简化它:
SELECT
a.product_id,
b.variation_id
FROM
(
SELECT
order_id,
meta_value as product_id
FROM
ctc_woocommerce_order_itemmeta
WHERE
meta_key = '_product_id'
AND order_id = 3988
LIMIT 1
) a
JOIN
(
SELECT
order_id,
meta_value as variation_id
FROM
ctc_woocommerce_order_itemmeta
WHERE
meta_key = '_variation_id'
AND order_id = 3988
LIMIT 1
) b
ON a.order_id = b.order_id;