2016-10-06 85 views
0

我有一個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的結果,而是整個結果爲空,並且不返回任何行。

+0

我的答案中的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

回答

1

而不是...

INNER JOIN wp_postmeta as wp11 
ON wp11.post_id=wp0.post_id 

WHERE ... 
AND wp11.meta_key='ex_center' 

你可以使用一個「外部聯接」操作,而不是「內部連接」,將狀態從WHERE子句的加入ON條款,並移除任何在從WP11任何列要求非NULL值的條件......

LEFT JOIN wp_postmeta as wp11 
ON wp11.post_id=wp0.post_id 
AND wp11.meta_key='ex_center' 

WHERE ... 

與外連接,如果沒有匹配的行從WP11發現,所有從WP11列將是NULL。 (這就是爲什麼我們需要將條件從WHERE子句移動到ON子句...... WHERE子句中的條件不能滿足NULL ...

對任何可以接受的表格重複相同的模式連續被「失蹤」。


其實,使用LEFT JOIN,你需要在「駕駛」榜第一,也許希望wp_posts wpp表先在FROM子句和參考wpp.id在外部連接條件我並不認爲需要wp0引用,你可以從wpp.id獲得post_id

SELECT 
    FROM wp_posts wpp 
    LEFT JOIN wp_postmeta wp1 ON wp1.post_id=wpp.id AND wp1.meta_key='_sale_price' 
    LEFT JOIN wp_postmeta wp2 ON wp2.post_id=wpp.id AND wp2.meta_key='...' 
    LEFT JOIN wp_postmeta wp3 ON wp3.post_id=wpp.id AND wp3.meta_key='...' 
    ... 
    LEFT JOIN wp_postmeta wp11 ON wp11.post_id=wpp.id AND wp11.meta_key='ex_center' 
WHERE wpp.id = 39 

我有一種感覺,你剛剛開始體驗使用EAV的歡樂。讓樂趣開始!


要回答你問...沒有,有沒有辦法讓一個AND condition圍繞這個問題,沒有修改的條件下,通過添加OR條件...

AND (condition OR some_other_condition) 

的MySQL有時在OR條件下表現不佳(性能方面)。

+0

spencer7593,請在我的問題中看到**** UPDATE ****,謝謝。 – rodney

+0

nerver-mind,我用OUTER代替了LEFT – rodney

+0

這個工作很完美,謝謝!! – rodney