在联接查询中使用倍数和条件

时间:2018-07-24 05:17:33

标签: php mysql join where-clause

如果一个帖子中的值不等于空白,并且两个帖子中的值都可以完美运行,我想加入查询。问题在于,当值仅在一个帖子中时,AND在其他任何第二个值之前。希望得到解决方案。我正在使用核心PHP

if(!empty($_POST['agent_wise'])){       
    $agent_wise = $_POST['agent_wise'];
    $queryCondition .= "allusers.sno ='$agent_wise'";
} 

if(!empty($_POST['status_wise'])){      
    $status_wise = $_POST['status_wise'];
    $queryCondition .= " AND st_application.admin_status_crs ='$status_wise'";
}   
if(!empty($_POST['course_wise'])){      
    $course_wise = $_POST['course_wise'];
    $queryCondition .= " AND st_application.prg_name1 ='$course_wise'";
}


$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where " . $queryCondition . " ";

5 个答案:

答案 0 :(得分:1)

尝试此代码。

if(!empty($_POST['agent_wise'])){      
    $agent_wise = $_POST['agent_wise'];
    $queryCondition .= "allusers.sno ='$agent_wise'";
 } 

if(!empty($_POST['status_wise'])){      
    $status_wise = $_POST['status_wise'];
    $queryCondition .= " AND st_application.admin_status_crs ='$status_wise'";
}   

if(!empty($_POST['course_wise'])){      
    $course_wise = $_POST['course_wise'];
    $queryCondition .= " AND st_application.prg_name1 ='$course_wise'";
}


$queryCondition = trim($queryCondition, 'AND'); 


$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where " . $queryCondition . " ";

我希望对您有帮助

答案 1 :(得分:0)

懒惰的黑客也将在第一个条件下也包含一个AND,并将WHERE更改为WHERE 1,但是有更聪明的,我怀疑是易于搜索的方式来解决此问题。哦,还可以看到有关sql注入和预准备语句的重要性

答案 2 :(得分:0)

这是解决方法...

a = data_Dub.groupby(['AwayTeam', 'SimStage'])['AwayPoint'].sum()
b = data_Dub.groupby(['HomeTeam', 'SimStage'])['HomePoint'].sum()
s = a.add(b).rename('Point')

df = s.sort_index(level=[1, 0]).reset_index().rename(columns={'AwayTeam':'Team'})
df = df[['Team','Point','SimStage']]
print (df)
  Team  Point  SimStage
0    A      4         0
1    B      4         0
2    C      0         0
3    D      1         0
4    E      4         0
5    A      4         1
6    B      4         1
7    C      0         1
8    D      1         1
9    E      4         1

答案 3 :(得分:0)

在确认“ AND”之前检查空字符串。

$queryCondition .=  (empty($queryCondition) ? "" : " AND ") . 
    "st_application.admin_status_crs ='$status_wise'"

答案 4 :(得分:0)

您的问题有两种解决方案。一种SQL解决方案和一种PHP解决方案:

第一个解决方案

if(!empty($_POST['agent_wise'])){       
    $agent_wise = $_POST['agent_wise'];
    $queryCondition .= " AND allusers.sno ='$agent_wise'";
} 

if(!empty($_POST['status_wise'])){      
    $status_wise = $_POST['status_wise'];
    $queryCondition .= " AND st_application.admin_status_crs ='$status_wise'";
}   
if(!empty($_POST['course_wise'])){      
    $course_wise = $_POST['course_wise'];
    $queryCondition .= " AND st_application.prg_name1 ='$course_wise'";
}


$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where TRUE" . $queryCondition . " ";

之所以可行,是因为在“真”和“其他”条件下,真条件是多余的。唯一的缺点是,如果不存在任何POST,则将返回所有记录,因为WHERE TRUE总是带回记录。但是您可以方便地进行检查以查看是否存在所有POST,如果没有,则可以进行纾困:

if (empty($_POST['agent_wise']) && empty($_POST['status_wise']) && empty($_POST['course_wise']) ) exit;

第二个解决方案

$queryConditionsArray = [];

if(!empty($_POST['agent_wise'])){       
    $agent_wise = $_POST['agent_wise'];
    $queryConditionsArray[] = "allusers.sno ='$agent_wise'";
} 

if(!empty($_POST['status_wise'])){      
    $status_wise = $_POST['status_wise'];
    $queryConditionsArray[] = "st_application.admin_status_crs ='$status_wise'";
}   
if(!empty($_POST['course_wise'])){      
    $course_wise = $_POST['course_wise'];
    $queryConditionsArray[] = "st_application.prg_name1 ='$course_wise'";
}

$queryCondition = implode(' AND ', $queryConditionsArray);

$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where " . $queryCondition . " ";

之所以可行,是因为内爆只会在条件之间插入单词“ AND”,而不是在每个条件的开头。再次,您将必须检查是否不存在POST,因为否则您的SQL将会失败。

我个人更喜欢第二种方式,但这取决于具体情况。