php+mysql两次左外联跨表查询
代码如下:
$querySel="select * from roomsy rsy left join room ro on rsy.RoomID=ro.ID left join hotel ho on rsy.HotelID=ho.ID where to_days(EndDate)-to_days(now())>=0"; if($StarLevel) $querySel = $querySel . " and StarLevel = '". $StarLevel ."'"; if($Key) $querySel = $querySel . " and ro.RName like '%". $Key ."%'"; $querySel = $querySel . " order by SOrder,rsy.ID desc"; $result = mysql_query($querySel) or die(mysql_error()); $total_records = mysql_num_rows($result); //取得总记录数
代码中红色和绿色部分分别表示了和两个表进行了左外联,进行外联接,要给表起个别名,如果某个字段名称只存在其中一个表中,直接引用就可以,如果名称在两个表中有冲突,那么引用字段名的时候要在前面加上“表的别名.”进行区分。
----------------
Lynx
The quieter you become,the more you are able to hear.