我在使用 IF 语句“选择”一些要运行的 MySQL 代码时遇到问题。这是我的代码:
if ($numRows == 0 && count($terms) == 3) {
$query2 .= " AND ( i.ING = '" . join("' OR i.ING = '", $terms) . "')". $queryType ." GROUP BY r.recipeID
HAVING COUNT(DISTINCT i.ING ) = 2)";
$termCount = 2;
}
if ($numRows == 0 && $termCount == 2) {
$query2 .= " AND ( i.ING = '" . join("' OR i.ING = '", $terms) . "')". $queryType ." GROUP BY r.recipeID
HAVING COUNT(DISTINCT i.ING ) = 1)";
这输出:
AND ( i.ING = 'wonton wrappers' OR i.ING = 'grape jelly' OR i.ING = 'lard') GROUP BY r.recipeID HAVING COUNT(DISTINCT i.ING ) = 2)
AND ( i.ING = 'wonton wrappers' OR i.ING = 'grape jelly' OR i.ING = 'lard') GROUP BY r.recipeID HAVING COUNT(DISTINCT i.ING ) = 1)
但是,我只希望第二行成为我要使用的查询的一部分;但是因为第一个 if 语句也总是正确的,所以它也输出该行。
所以我想基于这两个陈述的结果是:
AND ( i.ING = 'wonton wrappers' OR i.ING = 'grape jelly' OR i.ING = 'lard') GROUP BY r.recipeID HAVING COUNT(DISTINCT i.ING ) = 1)
这可能是我一直在看它太久的情况,并且无法注意到它是否是我忽略的一些最简单的事情。
有任何想法吗?
谢谢你。