我需要一些帮助来加入下面的查询,因为我相信这是超级重,而在其他循环内循环......一团糟,对吧?! :(
这样可行,但是如果有人可以帮助将其加入到一个查询中:
$query_1 = mysql_query("SELECT * FROM booking_reservation WHERE reservation_confirmed = '1' AND (reservation_name LIKE '$data%' OR reservation_phone LIKE '$data%' OR reservation_email LIKE '$data%')");
while ($row = mysql_fetch_array($query_1)) {
$calendar_id = $row['calendar_id'];
$query_2 = mysql_query("SELECT * FROM booking_calendars WHERE calendar_id = '$calendar_id'");
$row_2 = mysql_fetch_array($query_2);
$slot_id = $query_1['slot_id'];
$query_3 = mysql_query("SELECT * FROM booking_slots WHERE slot_id = '$slot_id'");
while ($row_3 = mysql_fetch_array($query_3)) {
echo '<tr><td>' . htmlentities($row2['slot_date']) . '</td><td>' . htmlentities($query_3['slot_time_from']) . '</td><td>' . htmlentities($query_1['reservation_name']) . '</td><td>' . htmlentities($query_3['calendar_title']) . '</td><td>' . htmlentities($query_1['reservation_phone']) . '</td><td>' . htmlentities($query_1['reservation_email']) . '</td></tr>';
}
}
答案 0 :(得分:0)
其中一个应该是您想要的,如果您希望获得仅存在于booking_calendars和booking_slots中的预订,则使用INNER JOIN
;如果您想要所有预订,则使用LEFT JOIN
,无论它们是否存在于其他表格
内部联接:
SELECT * FROM booking_reservation br
INNER JOIN booking_calendars bc ON br.calendar_id=bc.calendar_id
INNER JOIN booking_slots bs ON br.slot_id=bs.slot_id
WHERE br.reservation_confirmed = '1'
AND (br.reservation_name LIKE '$data%'
OR br.reservation_phone LIKE '$data%'
OR br.reservation_email LIKE '$data%')
左连接:
SELECT * FROM booking_reservation br
LEFT JOIN booking_calendars bc ON br.calendar_id=bc.calendar_id
LEFT JOIN booking_slots bs ON br.slot_id=bs.slot_id
WHERE br.reservation_confirmed = '1'
AND (br.reservation_name LIKE '$data%'
OR br.reservation_phone LIKE '$data%'
OR br.reservation_email LIKE '$data%')
一个重要的注意事项,建议不要使用SELECT *
,最好告诉mysql从每个表或您选择的表中需要哪些列。
注意:你应该听Raptor所说的,不推荐使用mysql_ *函数,并且会在以后的php版本中删除,你应该开始使用PDO或mysqli < / p>
答案 1 :(得分:0)
试试这个我希望这个查询对你有用
select * from booking_reservation br
join booking_calendars bc ON br.calendar_id=bc.calendar_id
join booking_slots bs ON bc.slot_id=bs.slot_id
where br.reservation_confirmed='1'
AND br.(reservation_name LIKE '$data%' OR br.reservation_phone LIKE '$data%' OR br.reservation_email LIKE '$data%')