简体   繁体   English

PHP:使用IF语句生成SQL difficulites

[英]PHP: Generating SQL with an IF Statement difficulites

I'm having a problem with using IF statements to 'choose' some MySQL code to run. 我在使用IF语句“选择”运行某些MySQL代码时遇到问题。 Here's my code: 这是我的代码:

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)";

This outputs: 这输出:

    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)

However, i'm only wanting the 2nd line to be part of the query i'm going to use; 但是,我只想让第二行成为我要使用的查询的一部分; but because the 1st if statement is always true aswell, it outputs that line too. 但因为第一个if语句总是如此,它也会输出该行。

So the result I'd like to have based on the two statements is: 所以基于这两个陈述我想得到的结果是:

AND ( i.ING = 'wonton wrappers' OR i.ING = 'grape jelly' OR i.ING = 'lard') GROUP BY r.recipeID HAVING COUNT(DISTINCT i.ING ) = 1)

It might be a case of i've been looking at it too long, and can't notice if it's some simplest thing i'm overlooking. 这可能是我一直在看它太久的情况,并且无法注意到它是否是我忽略的一些最简单的事情。

Any ideas? 有任何想法吗?

Thank you. 谢谢。

Assuming that this is the only code effected, I think your best bet is to simply replace if ($numRows == 0 && $termCount == 2) with elseif ($numRows == 0 && $termCount == 2) . 假设这是唯一受影响的代码,我认为最好的办法就是用elseif ($numRows == 0 && $termCount == 2)替换if ($numRows == 0 && $termCount == 2) elseif ($numRows == 0 && $termCount == 2) (Basically, make it an else if clause to exclude the possibility of dupes) (基本上,将其作为排除欺骗可能性的其他if子句)

I'd suggest splitting the logic like this: 我建议像这样拆分逻辑:

if ($numRows == 0) {
  $query2 .= " AND ( i.ING = '" . join("' OR i.ING = '", $terms) . "')$queryType GROUP BY r.recipeID";

  $query2 .= " HAVING COUNT(DISTINCT i.ING) = " . (count($terms) == 3 ? 2 : 1);
}

Taking advantage of the fact your branches are basically the same. 利用你的分支基本相同的事实。

In your first IF statement; 在您的第一个IF声明中; if it is TRUE (and you enter that statement) you are setting $termCount to 2. The second IF statement depends on $termCount == 2. So right now if $numRows is 0 then both statements will always be true. 如果它为TRUE(并输入该语句),则将$ termCount设置为2.第二个IF语句依赖于$ termCount == 2.因此,如果$ numRows为0,则两个语句将始终为true。 Find a different way to trigger the second IF statement; 找到另一种触发第二个IF语句的方法; or simply remove $termCount = 2 from the first IF statement 或者只是从第一个IF语句中删除$ termCount = 2

使用If else代替并形成您的语句,以便查询仅在逻辑上应该分配给$ query。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM