我想要条件“if”in mysql [已关闭]

1zmg4dgp  于 2023-01-20  发布在  Mysql
关注(0)|答案(1)|浏览(68)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
Helo,我如何做得更好这段代码:例如$lanyear具有查询中的值或者$lanterm也具有查询中的值以及其它参数

if ($lanyear == "" && $lanterm == "10" && $level == "" && $class == "") {
    $sql = "SELECT $field from `$table`";
} else if ($lanyear != "" && $lanterm == "10" && $level == "" && $class == "") {
    $sql = "SELECT $field from `$table` WHERE tyear='$lanyear'";
} else if ($lanyear == "" && $lanterm != "10" && $level == "" && $class == "") {
    $sql = "SELECT $field from `$table` WHERE term='$lanterm'";
} else if ($lanyear == "" && $lanterm == "10" && $level != "" && $class == "") {
    $sql = "SELECT $field from `$table` WHERE levelnum='$level'";
} else if ($lanyear == "" && $lanterm == "10" && $level == "" && $class != "") {
    $sql = "SELECT $field from `$table` WHERE classcode='$class'";
} else if ($lanyear != "" && $lanterm != "10" && $level == "" && $class == "") {
    $sql = "SELECT $field from `$table` WHERE tyear='$lanyear' AND term='$lanterm'";
} else if ($lanyear != "" && $lanterm == "10" && $level != "" && $class == "") {
    $sql = "SELECT $field from `$table` WHERE tyear='$lanyear' AND levelnum='$level'";
} else if ($lanyear != "" && $lanterm == "10" && $level == "" && $class != "") {
    $sql = "SELECT $field from `$table` WHERE tyear='$lanyear' AND classcode='$class'";
} else if ($lanyear == "" && $lanterm != "10" && $level != "" && $class == "") {
    $sql = "SELECT $field from `$table` WHERE term='$lanterm' AND levelnum='$level'";
} else if ($lanyear == "" && $lanterm != "10" && $level == "" && $class != "") {
    $sql = "SELECT $field from `$table` WHERE term='$lanterm' AND classcode='$class'";
} else if ($lanyear == "" && $lanterm == "10" && $level != "" && $class != "") {
    $sql = "SELECT $field from `$table` WHERE levelnum='$level' AND classcode='$class'";
} else if ($lanyear != "" && $lanterm != "10" && $level != "" && $class == "") {
    $sql = "SELECT $field from `$table` WHERE tyear='$lanyear' AND term='$lanterm' AND levelnum='$level'";
} else if ($lanyear == "" && $lanterm != "10" && $level != "" && $class != "") {
    $sql = "SELECT $field from `$table` WHERE term='$lanterm' AND levelnum='$level' AND classcode='$class'";
} else if ($lanyear != "" && $lanterm != "10" && $level != "" && $class != "") {
    $sql = "SELECT $field from `$table` WHERE tyear='$lanyear' AND term='$lanterm' AND levelnum='$level' AND classcode='$class'";
}

用条件“如果”可以吗?
请帮帮我。

ljsrvy3e

ljsrvy3e1#

如果这是PHP,你可以这样做:

$pt1 = $pt2 = $pt3 = $pt4 = '';
$ptLast = '1=1'; // so you dont have a stray AND

if ($lanyear != ""){    $pt1 = "tyear='$lanyear' AND ";}
if ($lanterm != "10"){  $pt2 = "term='$lanterm' AND ";}
if ($level != ""){      $pt3 = "levelnum='$level' AND ";}
if ($class != ""){      $pt4 = "classcode='$class' AND ";}

$sql = "SELECT $field from `$table` WHERE ".$pt1.$pt2.$pt3.$pt4.$ptLast

相关问题