我正在尝试构建一个SQL SELECT查询,该查询从两个不同的表中获取信息并按订单ID对它们进行分组。目前,我已经了解了这个问题:
SELECT T1.order_item_id, T2.order_item_id, T2.order_id, T3.post_id,
CASE WHEN T1.meta_key = 'Name' and T2.order_item_id = T1.order_item_id
THEN T3.meta_value
END as name,
CASE WHEN T3.meta_key = '_billing_address' and T2.order_id = T3.post_id
THEN T3.meta_value
END as billing_address,
CASE WHEN T3.meta_key = '_billing_email' and T2.order_id = T3.post_id
THEN T3.meta_value
END as billing_email
FROM wp_woocommerce_order_itemmeta T1
LEFT JOIN wp_woocommerce_order_items T2
ON T1.order_item_id = T2.order_item_id
LEFT JOIN wp_postmeta T3
ON T2.order_id = T3.post_id
以下是我从中获取的表格:
T1:
+---------------------------------------+
| order_item_id | meta_key | meta_value|
+---------------------------------------+
| 1 | Name | John Doe |
+---------------------------------------+
T2
+---------------------------------------+
| order_item_id | order_id |
+---------------------------------------+
| 1 | 101 |
+---------------------------------------+
T3
+-------------------------------------------------+
| post_id | meta_key | meta_value |
+-------------------------------------------------+
| 101 | _billing_address | 123 Fake St. |
| 101 | _billing_email | jondoe@gmail.com |
+-------------------------------------------------+
我希望能够创建一个如下所示的表:
+-------------------------------------------------------+
| order_id | name | address | email |
+-------------------------------------------------------+
| 101 | John Doe | 123 Fake St. | jondoe@gmail.com |
+-------------------------------------------------------+
但是上述SQL查询产生的表有很多用于帐单地址,名称和电子邮件的空值,而且似乎并没有从单元格中获取数据。
我觉得CASE表达可能是个问题,但我并不完全确定。
答案 0 :(得分:1)
你有条件部分,现在使用UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { requests in
for request in requests {
print(request.trigger)
}
})
添加一些聚合来通过条件聚合来解决这个问题:
max()
看起来您也可以像这样简化案例陈述:
SELECT
T1.order_item_id
, T2.order_item_id
, T2.order_id
, T3.post_id
, max(CASE WHEN T1.meta_key = 'Name' and T2.order_item_id = T1.order_item_id
THEN T3.meta_value
END) as name
, max(CASE WHEN T3.meta_key = '_billing_address' and T2.order_id = T3.post_id
THEN T3.meta_value
END) as billing_address
, max(CASE WHEN T3.meta_key = '_billing_email' and T2.order_id = T3.post_id
THEN T3.meta_value
END) as billing_email
FROM wp_woocommerce_order_itemmeta T1
LEFT JOIN wp_woocommerce_order_items T2
ON T1.order_item_id = T2.order_item_id
LEFT JOIN wp_postmeta T3
ON T2.order_id = T3.post_id
group by
T1.order_item_id
, T2.order_item_id
, T2.order_id
, T3.post_id
答案 1 :(得分:0)
我认为如果您在选择的postmeta表上两次加入联接,则查询会更容易:
SELECT T1.order_item_id, T2.order_item_id, T2.order_id, T3.post_id,
T1.meta_key as name,
T3A.meta_key as address,
T3E.meta_key as email,
FROM wp_woocommerce_order_itemmeta T1
LEFT JOIN wp_woocommerce_order_items T2
ON T1.order_item_id = T2.order_item_id
LEFT JOIN wp_postmeta T3A
ON T2.order_id = T3A.post_id AND T3A.meta_key = '_billing_address'
LEFT JOIN wp_postmeta T3E
ON T2.order_id = T3E.post_id AND T3E.meta_key = '_billing_email'
注意:此查询未经过测试,但您可以提供示例数据库