[英]mysql conditional “not_in” based on another value to parse array into query
我有以下數組
$exclude = array(1 => array(1,2,3,4), 2 => array(1,2,3,4));
驗證密鑰時,我需要排除內部數組中的值。 我不確定是否應該使用CASE
或僅使用AND
和OR
。
這是我嘗試的開始,但是我錯過了一些東西,因為它無法按預期工作。.我將查詢追加到WHERE
子句的末尾,因此這是我唯一可以訪問的部分。
foreach($exclude as $blogID => $post_ids) {
$ids = implode(',', $post_ids);
if($blogID == 1) {
$where .= " AND table.BLOG_ID = {$blogID} AND table.ID NOT IN ($ids)";
} else {
$where .= " OR table.BLOG_ID = {$blogID} AND table.ID NOT IN ($ids)";
}
}
我不確定是否需要其余的查詢進行試驗,但這可能有助於在所有子句周圍添加一些括號以確保所有AND和OR均按預期進行交互。 像這樣:
if ($exclude) {
$where .= ' AND ('; // Enclose all the ORs in one set of parentheses
$or = '';
foreach($exclude as $blogID => $post_ids) {
$ids = implode(',', $post_ids);
// Each individual OR part is enclosed in parentheses
$where .= "$or(table.BLOG_ID = {$blogID} AND table.ID NOT IN ($ids))";
$or = ' OR ';
}
$where .= ")";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.