2016-08-16 107 views
0

我試圖從woocommerce中抽出一些數據,並有90%的我想工作得很好,但是postmeta正在殺死我。我試圖獲取與帖子相關的postmeta條目的子集,而不是所有這些條目,如果我只選擇一個meta_key,它可以正常工作,但倍數會從表中返回所有meta_key/meta_value對,無論post_id如何。我不是一個dba,我知道有足夠的SQL讓自己陷入困境。我已經完成了。現在我正在尋找一些指導。謝謝。基於其他列的值獲取MySQL列根據其他列的值

select meta_key, meta_value from wp_postmeta where post_id=1923 
AND meta_key='_billing_first_name' 
OR meta_key='_billing_last_name' 

回答

1

or準則否定post_id=1923。你可以使用括號:

select meta_key, meta_value 
from wp_postmeta 
where post_id=1923 and 
    (meta_key='_billing_first_name' or meta_key='_billing_last_name') 

但使用in可能會是你最好的選擇:

select meta_key, meta_value 
from wp_postmeta 
where post_id=1923 and 
    meta_key in ('_billing_first_name', '_billing_last_name') 
+0

啊,太棒了,這做到了!謝謝! –