2012-06-14 70 views
2

希望這只是語法的一種情況。從MySQL查詢中排除多個ID

我正在爲Wordpress編寫一個自定義搜索功能,除非我想排除依賴於ID的幾個結果,否則它的運行效果很好。

該工程確定一個ID

$sqlp_page ="select ID, post_title, post_name, post_excerpt from wp_posts where post_type='page' and ID != '236' "; 
       $sqlp_page .="and post_status='publish' "; 
       $sqlp_page .="and (post_title like '%".$_GET['s']."%' "; 
       $sqlp_page .="or post_content like '%".$_GET['s']."%') "; 
       $sqlp_page .="and post_status='publish' "; 
       $sqlp_page .="order by id ASC "; 

但我似乎無法爲ID不止一個值傳遞。 我已經搜索了網絡,並嘗試了幾種不同的方式,但似乎沒有爲我工作。

    $sqlp_page ="select ID, post_title, post_name, post_excerpt from wp_posts where post_type='page' and ID != '236,239' "; 

或者

$sqlp_page ="select ID, post_title, post_name, post_excerpt from wp_posts where post_type='page' and ID != '236' or '239' "; 

而且

$sqlp_page .="and ID != '236' "; 
$sqlp_page .="and ID != '239' "; 

但似乎沒有任何工作。 任何援助非常感謝。

+1

你不能使用'WHERE ID IN('1','5','235');' – Bob

回答

5

使用NOT IN

$sqlp_page ="select ID, post_title, post_name, post_excerpt 
from wp_posts where post_type='page' and ID NOT IN ('236','239') "; 

裏面NOT IN,需要多個ID值用逗號分隔。

+0

乾杯!很棒! – user892670