[英]Mysql complex query to join
I need some help to JOIN the queries below, because I believe this is super heavy, While loops inside other while loops... a mess, right?! 我需要一些帮助来加入下面的查询,因为我认为这非常繁琐,而while循环又在while循环内...乱七八糟,对吗? :( :(
This works, but if someone could help to JOIN this into one query only: 这可行,但是如果有人可以帮助将其仅加入一个查询中:
$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>';
}
}
One of these should be what you want, you use INNER JOIN
if you want to get reservations that only exist in booking_calendars, and booking_slots, or LEFT JOIN
if you want all reservations regardless if they exist in the other tables 其中之一应该是您想要的,如果要获取仅存在于booking_calendars和booking_slots中的预订,则可以使用INNER JOIN
如果要获取所有预订(无论它们是否存在于其他表中),请使用LEFT JOIN
Inner 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%')
Left join: 左联接:
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%')
1 important note, using SELECT *
isn't recommended, it is much better to tell mysql which columns you need from each of the tables, or from the table you're selecting from. 1个重要说明,不建议使用SELECT *
,最好告诉mysql每个表或从中选择的表中需要哪些列。
NB: You should listen to what Raptor said, mysql_* functions are deprecated and will be removed in later php versions, you should start using PDO or mysqli 注意:您应该听听Raptor所说的话,不建议使用mysql_ *函数,并且将在以后的php版本中将其删除,您应该开始使用PDO或mysqli
Try with this i hope this query will works for you 试试这个,我希望这个查询对你有用
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%')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.