sql如何获得where =“all”
问题描述:
嗨,我想在我的网站中创建一个搜索框我已经完成了所有事情,但我想知道我有3个搜索框,第一个是城市第二个是位置和第三种是类型。它在用户选择所有3个框中的某些内容时起作用,但是我想知道当用户在任何一个或多个框中选择ALL时使用的SQL查询。 例子 - 这是我在做什么sql如何获得where =“all”
$sql = "SELECT * FROM employe WHERE date=$date AND reason='$reason' AND month='$month' ";
$result = mysqli_query($conn, $sql);
这是我想
$sql = "SELECT * FROM employe WHERE date='ALL' AND reason='ALL' AND month='$month' ";
$result = mysqli_query($conn, $sql);
它应该是$日期有,但只是为了说明我已经写都让你们能明白我想要什么
答
尝试构建条件字符串并将其用于sql语句。这样
if($date=='ALL' or $date==''){
$datecond='';
}
else{
$datecond = " and date='$date' ";
}
if($reason=='ALL' or $reason==''){
$reasoncond='';
}
else{
$reasoncond=" and reason='$reason'";
}
if($month=='ALL' or $month==''){
$monthcond = '';
}
else{
$monthcond = " and month='$month'";
}
$sql = "select * from employee where 1 $datecond $reasoncond $monthcond";
如果你想给用户搜索** **所有的记录,这是一样的没有过滤所有的能力,所以你只需要删除'原因=“$ reason''从你的查询(如果'$ reason =='ALL'')。 – Dekel
是这样,如果我将它的工作 ALL –
然后我必须运行多个查询,如果我使用$ reason =='ALL',因为有一个概率,用户选择所有在第一个盒子或第二个盒子或第三个盒子或任何两个盒子或没有盒子或所有盒子 –