duangu3620 2015-12-09 11:21
浏览 202
已采纳

MySQL - 如果键/值对不存在,则忽略WHERE条件

In a Wordpress order based system I am selecting people going to an event on a certain date and location.

I also need to see if there are any order notes existing for any matching orders, which I do by selecting the meta_value where the meta_key ="_wc_acof_2".

The problem is when I add the condition to my WHERE clause the code stops returning records where that meta key/value pair doesn't exist (i.e. there are no order notes). Whereas I still need to select those records just return null or similar for the order notes.

The problem line in the WHERE clause is:

AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"

In my head I would like to write that line similar to:

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')

The full query:

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

Any advice much appreciated!

  • 写回答

2条回答 默认 最新

  • dpa55065 2015-12-09 18:12
    关注

    The answer is to put the extra bit in a sub-select like so:

    (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条)

报告相同问题?

悬赏问题

  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!