2012-12-07 37 views
0

下面是我在MySQL數據庫表結構 - 我要相互匹配特定的動態字段值,如下所述:PHP MySQL查詢,以配合其他基於領域的動態匹配值條件動態字段值

meta_id post_id  meta_key   meta_value 
616  472   "machine_type"  "Combined pipe cutting" 
561  472   _edit_lock   1354882015:1 
560  472   _edit_last   1 
562  472   _mf_write_panel_id 1 
565  472   _wp_page_template machine_layout.php 
617  472   jet_filter   
618  472   "length_x_axis"  "3000mm" 
619  472   machine_image  488 

上面我有""引用標記的字段 - 我必須應用查詢和獲取結果。

現在在這裏,我想檢查meta_key字段和meta_value字段,並且想要檢索匹配的post_id字段的值。

對於例如:我在meta_key第一個參數將是length_x_axismeta_value其匹配的值將是3000mmAND條件與它會meta_keymachine_typemeta_value其匹配的值將是Combined pipe cutting

而我需要的結果是post_id472

這是一個完整的動態的WordPress wp_postmeta表,這將有這樣的條件下對多條記錄。

最早的MySQL查詢結果將不勝感激。

請原諒我是否有任何拼寫錯誤,我盡力解釋我的問題。

請幫忙!

回答

1

我不知道你真正想要的,但我認爲這是你正在尋找的查詢:

SELECT t1.post_id 
FROM mytable t1 inner join mytable t2 on t1.post_id = t2.post_id 
WHERE t1.meta_key = "length_x_axis" and t1.meta_value="3000mm" 
AND t2.meta_key="machine_type" and t2.meta_value="Combined pipe cutting" 
+0

這個工程的要求,非常感謝! –