使用多个变量进行Like查询。请帮助我缩短查询时间

时间:2017-06-02 07:02:28

标签: php mysql sql mysqli

这就是我的SQL查询

$stmt = $con->prepare("select * from fistevent WHERE  row_name LIKE 'A%' And Event_Id =? OR row_name LIKE 'B%' And Event_Id =? OR row_name LIKE 'C%' And Event_Id =? OR row_name LIKE 'D%' And Event_Id =? OR row_name LIKE 'E%' And Event_Id =? order by row_name ASC");
$stmt->bind_param("s", $_POST['EventId']);
        $stmt->execute();
        $result = $stmt->get_result();

如何将数据库中的值与数组进行比较来自我的代码中的html

$TicketType =$_POST['Tickettype'];
$seatS=$_POST['finalSeats'];
$EventId=$_POST["Eventid"];
$sqlData = array();

从数据库中获取结果

$stmt = $con->prepare("SELECT * FROM fist WHERE `Id`=? AND `Type`=?") or die($con->error);

绑定结果

$stmt->bind_param("ss",$EventId , $TicketType);
$stmt->execute();
$stmt->store_result();
$stmt-> bind_result($id,$Event_Id,$TicketType,$seats);
if($stmt->fetch())
{
$data[] = array($id,$Event_Id,$TicketType,$seats);
$Tickettype=$TicketType;
$Rowname=$row_name;
$Seats=$seats;
$cats = array_filter(array_map('trim', explode(',', $seatS)));
foreach($cats as $key => $cat ) {

在这里,我想比较$ cat与数据库值$ Seats但

while($Seats==$cat)
{
echo "already hold";
}
else
{
echo "hold the seats";
}
}
}

1 个答案:

答案 0 :(得分:1)

看起来你有相同的event_id所以你可以使用像

这样的查询
select * from fistevent WHERE (row_name LIKE 'A%' OR row_name LIKE 'B%' OR row_name LIKE 'C%' OR row_name LIKE 'D%' OR row_name LIKE 'E%') And Event_Id =? order by row_name ASC