我有一個MySQL查詢獲取高級自定義字段數據的Wordpress。如果其中一個AND行沒有結果,則整個查詢不會提供結果。我想知道是否可以添加一些東西到查詢中,如果沒有AND行的結果,會忽略AND行?MySQL - 忽略和如果沒有值
SELECT DISTINCT post_title,
wp0.post_id as the_post_id,
wp1.meta_value as _sale_price,
wp2.meta_value as _regular_price,
wp3.meta_value as ex_diameter,
wp4.meta_value as ex_center,
wp5.meta_value as ex_length,
wp6.meta_value as ex_alloy,
wp7.meta_value as ex_butt_1,
wp8.meta_value as ex_butt_2,
wp9.meta_value as ex_taper_1,
wp10.meta_value as ex_taper_2,
wp11.meta_value as ex_center
from wp_postmeta as wp0
INNER JOIN wp_postmeta as wp1
ON wp1.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp2
ON wp2.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp3
ON wp3.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp4
ON wp4.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp5
ON wp5.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp6
ON wp6.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp7
ON wp7.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp8
ON wp8.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp9
ON wp9.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp10
ON wp10.post_id=wp0.post_id
INNER JOIN wp_postmeta as wp11
ON wp11.post_id=wp0.post_id
INNER JOIN wp_posts as wpp
ON wp1.post_id=wpp.ID
WHERE wp1.post_id='39'
AND wp1.meta_key='_sale_price'
AND wp2.meta_key='_regular_price'
AND wp3.meta_key='ex_diameter'
AND wp4.meta_key='ex_center'
AND wp5.meta_key='ex_length'
AND wp6.meta_key='ex_alloy'
AND wp7.meta_key='ex_butt_1'
AND wp8.meta_key='ex_butt_2'
AND wp9.meta_key='ex_taper_1'
AND wp10.meta_key='ex_taper_2'
AND wp11.meta_key='ex_center'
例如; wp3.meta_key ='ex_diameter'可能根本不存在,但是不會得到帶有空白ex_diameter的結果,而是整個結果爲空,並且不返回任何行。
我的答案中的SQL示例顯示**'LEFT JOIN' ** * not * **'OUTER JOIN' ** **。另外,我想我會希望**'wpp' **成爲駕駛臺。我會首先在'FROM'子句中列出...並執行'WHERE wp.id ='39''。我會按照帶有一系列外部連接的'FROM wpp' ... **'LEFT JOIN wp_postmeta wp1 ON wp1.post_id = wpp.id AND wp1.meta_key ='_ sale_price'' **。我沒有看到'wp0'的必要,你可以從'wpp.id'獲得'post_id'。 – spencer7593