mysql 条件 "not_in" 基于另一个值将数组解析为查询
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 .= ")";
}
我有以下数组
$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 .= ")";
}