我正在尝试确定与论坛相关的主题组合是否唯一。这是在向论坛添加主题时完成的。使用此代码和查询检查唯一性:
$options = array(); //here's your choices
$options[] = 'blablabla';
$options[] = 'blabla';
foreach($options as $key => $value)
{
echo '<li>' . $value . '</li>';
}
$sql_unique = "SELECT Forums_ForumID, list
FROM (
SELECT Forums_ForumID, GROUP_CONCAT( Topics_TopicID ) AS list
FROM (
SELECT *
FROM Topics_crosstable
ORDER BY Topics_TopicID
)H
GROUP BY Forums_ForumID
)A
WHERE list = (
SELECT GROUP_concat( TopicID )
FROM Topics
WHERE Name IN (";
$sql_unique .= implode(",",$options);
$sql_unique .= ") ORDER BY Forums_ForumID ASC )";
$result = mysql_query($sql_unique);
//print "$result";
//echo $result;
//echo mysql_num_rows($result);
//$assoc = mysql_fetch_assoc($result);
var_dump($result);
我确信在 WHERE 中使用固定值时查询可以正常工作。使用当前代码我无法获得任何输出。无论主题组合是否唯一,vardump 都会给出结果“假”。
如果选项是字符串,则必须引用它们。
$sql_unique .= "'". implode("','", $options) ."'";