【问题标题】:where clause with not null variables带有非空变量的 where 子句
【发布时间】:2014-10-26 07:45:33
【问题描述】:

下面的一些变量(us1 或 sp1..)可能有空值(客户有可能不填写相应的文本视图)。我如何仅使用“not null”变量来语法“where”子句;例如,当 us1 不为空且 sp1 为空时,“where”子句必须为:“ where customer.username1 = 'us1'” 提前致谢

$us1 = $_POST['username1'];
$sp1 = $_POST['startPoli1'];

SELECT `username1`,`startPoli1`, `finalPoli1`,`weight1` ,`phone1` 
 FROM customer ,registration1 
 where  customer.startPoli1 = 'sp1' and customer.username1 = 'us1'

【问题讨论】:

标签: php mysql sql


【解决方案1】:

条件这样写:

WHERE ('sp1' IS NULL OR customer.startPoli1 = 'sp1')
AND ('us1' IS NULL OR customer.username1 = 'us1')

【讨论】:

    【解决方案2】:

    像这样:

    $us1 = $_POST['username1'];
    $sp1 = $_POST['startPoli1'];
    
    $sql = "SELECT `username1`,`startPoli1`, `finalPoli1`,`weight1` ,`phone1` 
     FROM customer ,registration1 where 1=1 ";
    
    if($sp1 != null)
       $sql += "and customer.startPoli1 = 'sp1'";
    if($us1 != null) 
       $sql += "and customer.username1 = 'us1'";
    

    【讨论】:

      【解决方案3】:

      我认为 - 这是有问题的错误。忘记了 $sp1 和 $us1 是变量

      $us1 = $_POST['username1'];
      $sp1 = $_POST['startPoli1'];
      
      $sql = "SELECT `username1`,`startPoli1`, `finalPoli1`,`weight1` ,`phone1` 
       FROM customer ,registration1 where 1=1 ";
      
      if($sp1 != null)
         $sql += "and customer.startPoli1 = '" + $sp1 + "'";
      if($us1 != null) 
         $sql += "and customer.username1 = '" + $us1 + "'";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-11
        • 2014-07-29
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 2023-03-17
        相关资源
        最近更新 更多