【发布时间】:2019-08-24 02:17:36
【问题描述】:
我想从 3 个不同的表格中选择不同的列并显示在表格数据中。
我有一张名为 room 的桌子。因为我有两个名为 Hotel_id 和 Room_Type_id 的外键。房间表中的其他列是价格、设施等。我想使用 FK hotel_id 从酒店表中获取酒店名称,并使用 FK Room_Type_id 从房间类型表中获取房间类型名称。
我进行了很多头脑风暴,并使用连接进行单独查询以查找 hotel_name 和 room_type_name。使用以下查询
//Query to find hotel_name
$query = "SELECT hotels.hotel_name FROM hotels INNER JOIN room ON room.Hotel_Id=hotels.hotel_id";
$res = mysqli_query($connection, $query);
while ($data1 = mysqli_fetch_array($res)) {
//Query to find room_type_name:
$sel_cus = "SELECT room_type.Room_Type_Name FROM room INNER JOIN room_type ON room.room_type=room_type.Room_Type_Id";
$res_cus = mysqli_query($connection, $sel_cus);
while ($data = mysqli_fetch_array($res_cus)) {
//Query to select price,and picture of room:
$count=1; // counter to print serial numbers in table
$sel_cus = "select * from room Order by room_id ASC";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
// here is code to print record in table
<tbody>
<tr>
<td><?php echo $count; ?></td>
<td ><?php echo $data1['hotel_name'];?></td>
<td><?php echo $data['Room_Type_Name']; ?></td>
<td><?php echo $row['price']; ?></td>
} // End Query to find hotel_name
} // End Query to find room_type_name:
} // end of Query to select price,and picture of room
上面的代码根据记录在房间表中迭代酒店名称和房间类型名称目前有2条记录在房间表中它迭代记录2次,请帮我解决这个问题。
以上代码输出:
Sr hotel_name Roomtype price picture
1 ABC Delux 600 a.jpg
2 ABC Delux 600 a.jpg
3 XYZ Super 8000 bcv.jpg
4 XYZ Super 8000 bcv.jpg
I want the output like below
Sr hotel_name Roomtype price picture
1 ABC Delux 600 a.jpg
2 XYZ Super 8000 bcv.jpg
请帮忙
【问题讨论】: