如何在php中的select where where where语句中使用数组?

时间:2015-05-14 17:22:46

标签: php mysql

我有这样的代码:

$sql11 = "select room_no from guestrocordtransc where roomtype='".$roomtype."'";
$retval11 = mysql_query( $sql11, $conn );
while($row11 = mysql_fetch_array($retval11,MYSQL_ASSOC))
{ 
    $cid_room=$row11['roomno']; 
}

这里我得到一组房间号码,存储在$cid_room

现在,我需要在select查询中使用$cid_room来获取不在guestrocordtransac中的特定room_no。

我的第二个查询如下:

$sql = "select room_no 
FROM roominfo WHERE room_no 
NOT IN(
select roomno 
from guestrocordtransac 
where roomtype='".$roomtype."' 
between '".$check_in."' 
and '".$check_out."' 
and cid='".$cid_room."') 
and roomtype='".$roomtype."'";

是否可以在查询中给出这样的数组值($cid_room),它是否会检查所有值。

3 个答案:

答案 0 :(得分:1)

如果$ cid_room不是多维数组,则在选择查询中使用数组,如下所示:

"SELECT room_no FROM roominfo WHERE room_no NOT IN (".implode(',',$cid_room).") AND roomtype='".$roomtype."'";

但我不确定你的情况是否有效。

答案 1 :(得分:0)

您可以将数组转换为IN()子句。

$in = '(';
foreach($cid_room as $num){
  $in.= "'$num',"
}
$in = substr($sql,0,-1) . ') ';  // remove trailing comma and close

答案 2 :(得分:0)

1:while($row11 = mysql_fetch_array($retval11,MYSQL_ASSOC)) { $cid_room=$row11['roomno']; }

由此你没有获得$cid_room

的数组

你必须使用$cid_room[]而不是$ cid_room

  1. $sql = "select room_no FROM roominfo WHERE room_no NOT IN( select roomno from guestrocordtransac where roomtype='".$roomtype."' between '".$check_in."' and '".$check_out."' and cid in '".$cid_room."') and roomtype='".$roomtype."'";