在基于Wordpress订单的系统中,我选择的人在某个日期和地点参加活动。
我还需要查看是否存在任何匹配订单的订单备注,我选择meta_value
所在的meta_key ="_wc_acof_2"
。
问题是当我将条件添加到我的WHERE子句时,代码停止返回不存在元键/值对的记录(即没有订单备注)。而我仍然需要选择那些记录,只为订单备注返回null或类似记录。
WHERE子句中的问题行是:
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
在我的脑海中,我想写出类似于:
的那一行AND (jn_postmeta_guestnotes.meta_key = "_wc_acof_2" OR 'there is no matching meta_key so just ignore this condition and return the record anyway')
完整查询:
SELECT
CONCAT_WS(" ",wp_postmeta.meta_value,jn_postmeta_lastname.meta_value) AS Name,
wp_woocommerce_order_itemmeta.meta_value AS Adults,
jn_postmeta_guestnotes.meta_value AS Notes
FROM
wp_postmeta
LEFT JOIN
wp_postmeta AS jn_postmeta_lastname ON wp_postmeta.post_id = jn_postmeta_lastname.post_id
LEFT JOIN
wp_postmeta AS jn_postmeta_guestnotes ON wp_postmeta.post_id = jn_postmeta_guestnotes.post_id
LEFT JOIN
wp_posts ON wp_postmeta.post_id = wp_posts.ID
LEFT JOIN
wp_woocommerce_order_items ON wp_woocommerce_order_items.order_id = wp_posts.ID
LEFT JOIN
wp_woocommerce_order_itemmeta ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
LEFT JOIN
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_location ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_location.order_item_id
LEFT JOIN
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_date ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_date.order_item_id
WHERE
1 = 1
AND wp_postmeta.meta_key = "_billing_first_name"
AND jn_postmeta_lastname.meta_key = "_billing_last_name"
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
AND wp_woocommerce_order_itemmeta.meta_key = "Adults"
AND jn_woocommerce_order_itemmeta_location.meta_key = "Booking Type"
AND jn_woocommerce_order_itemmeta_location.meta_value LIKE "%'.$showlocation.'%"
AND jn_woocommerce_order_itemmeta_date.meta_key = "Booking Date"
AND (jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformata.'" OR jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformatb.'")
AND wp_posts.post_status = "wc-completed"
GROUP BY wp_posts.ID
任何建议都非常感谢!
答案 0 :(得分:0)
答案是将额外的位放在子选择中,如下所示:
(SELECT wp_postmeta.meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = "_wc_acof_2" AND wp_postmeta.post_id = wp_posts.ID) AS Notes
答案 1 :(得分:0)
将所有过滤条件放在ON
上,以便左连接起作用。在WHERE
上将删除空值
.....
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_date
ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_date.order_item_id
AND 1 = 1
AND wp_postmeta.meta_key = "_billing_first_name"
AND jn_postmeta_lastname.meta_key = "_billing_last_name"
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
AND wp_woocommerce_order_itemmeta.meta_key = "Adults"
AND jn_woocommerce_order_itemmeta_location.meta_key = "Booking Type"
AND jn_woocommerce_order_itemmeta_location.meta_value LIKE "%'.$showlocation.'%"
AND jn_woocommerce_order_itemmeta_date.meta_key = "Booking Date"
AND
(
jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformata.'"
OR
jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformatb.'"
)
AND wp_posts.post_status = "wc-completed"