我有两张桌子。
如果我发出以下SQL语句:
Select roomname from Booking where checkin='12/03/2014' and checkout='15/03/2014'
我得到两行
除了102号房间和201号房间外,我想使用这些行来获取Rooms表格中的所有其他房间名称。
这是我的代码:
$a1=12/03/2014
$a2=15/03/2014
$sql= mysql_query("SELECT * FROM Booking WHERE checkin = '$a1' AND checkout ='$a2'");
while ($myrow = mysql_fetch_array($sql))
{
$pid = $myrow["roomname"];
$sql1= mysql_query("SELECT * FROM Rooms WHERE roomname != '$pid'");
while($me = mysql_fetch_array($sql1))
{
$pid2 = $me["roomname"];
echo $pid2;
}//
}//end while
我将获得两组结果,第一组包含201室,第二组包含102室:
Room 111 Room 201 Room122
Room 111 Room 102 Room122
我只想要一个不包含201室和102室的结果:
Room 111 and Room 102
答案 0 :(得分:2)
您可以尝试以下查询:
SELECT roomname FROM Booking WHERE roomname NOT IN (SELECT roomname FROM Booking WHERE checkin = '12/03/2014' AND checkout = '15/03/2014');
或至少它应该指向正确的方向。
答案 1 :(得分:0)
你能做的事情不仅仅是:
SELECT Rooms.* FROM Rooms
LEFT JOIN Booking
ON Rooms.roomname = Booking.roomname
WHERE checkin != '12/03/2014' AND checkout != '15/03/2014';