Php IF语句、IF函数或CASE?

Php IF语句、IF函数或CASE?,php,mysql,sql,if-statement,case,Php,Mysql,Sql,If Statement,Case,我有一些MySQL代码,我正在努力工作,但我不完全确定是否应该使用IF语句、IF函数或CASE操作符来满足我的需要。我一直对我在网上读到的东西感到困惑,我找不到任何地方可以简单地说明如何使用正确的语法而不变得复杂,所以如果这似乎是一个愚蠢的问题,我道歉 此代码在这里起作用: SELECT email, accountgroup, othercode FROM (SELECT email AS email, accountgroup, othercode

我有一些MySQL代码,我正在努力工作,但我不完全确定是否应该使用IF语句、IF函数或CASE操作符来满足我的需要。我一直对我在网上读到的东西感到困惑,我找不到任何地方可以简单地说明如何使用正确的语法而不变得复杂,所以如果这似乎是一个愚蠢的问题,我道歉

此代码在这里起作用:

SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '$row3' AND othercode = '$row4';
$row3和$row4存储一个POST的用户定义值。这些值将与数据库中某个列的值匹配,但是我还向这个POST'd数据添加了一个-all-and-none-value,以便用户可以选择-all-or-none-accountgroups。我的问题是,如果用户选择-all或-none,我不确定在SQL中该做什么。我原以为IF语句可以解决这个问题,但我的SQL出现了一个错误,我很确定我没有正确地使用IF语句

IF ($row3= '-all-') THEN
SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup <> '' AND othercode = '$row3';





ELSEIF ($row3 = '-none-') THEN
SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '' AND othercode = '$row4';






ELSE 
SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '$row3' AND othercode = '$row4';

  END IF

非常感谢您提供的任何帮助,以及以新手方式解释IF语句/函数的任何来源

这几乎取决于您-使用您想要的,但通常情况下,在可能存在多种情况时使用CASE,例如:

CASE ( value ) {
  $a = 'a';
  $a = 'b'
  $a = 'c'
  $a = 'd'
  $a = 'e'
  etc.
}
这样的话,箱子看起来会更好;而且添加额外字段也会更容易

如果您只有两个或多个选项或多个选项,请使用“如果”:

IF ( $a = a ) {
  IF ( $c = 2 && $d = 4 ) {
    $b = 1
  } ELSE {
    $b = 2
  }
} ELSE {
  $b = 3
}
一般来说。但是有很多选择如何以及何时使用witch one


PS:显示的数据只是简单的示例,与语法无关

如果需要,您可以在查询的某些列中使用用例或IF。但在本例中,您有整个SQL查询的条件。只需在PHP中处理这个条件,就不需要在SQL语句中处理了

据我所知,在php中使用它,并使用switch case,因为它比if-else条件快得多

关于这一点的更多信息是,如果您真的想在sql中包含条件,那么请使用用例

为了更好地了解mysql中的case语句

http://dev.mysql.com/doc/refman/5.0/en/case.html
http://dev.mysql.com/doc/refman/5.0/en/if.html
为了更好地了解mysql中的if语句

http://dev.mysql.com/doc/refman/5.0/en/case.html
http://dev.mysql.com/doc/refman/5.0/en/if.html

谢谢,这很有帮助!是的,从另一个角度来看,它使PHP的使用变得更加容易。谢谢你的意见。