PHP 在一个查询中显示来自四个表的数据(即:LEFT JOIN)
PHP displaying data from four tables in one query (ie: LEFT JOIN)
我有三个 table:
我想显示 'Event Details',它显示参加员工的详细信息(来自 'attending_employees table > 相应员工详细信息表格的员工 ID 列表 'employee' table),他们属于哪个团队(来自 club_teams table)以及来自 'club_events' table 的活动详情)。
目前我正在使用多个 mysqli 查询来显示此信息,但是无法在一个查询中从数据库中提取数据(即:LEFT JOIN)。非常感谢您的帮助!
以下是我目前正在使用的查询:
$query = msqli_query($con, "SELECT * FROM attending_employees")or die(mysqli_error($con));
if(mysqli_num_rows($query) > 0){
while($attending = mysqli_fetch_array($query)){
foreach($attending['club_event']){
$eventid = $attending['club_event'];
$query = msqli_query($con, "SELECT * FROM club_events WHERE club_event_id = '$eventid'")or die(mysqli_error($con));
while($event_details = mysqli_fetch_array($query)){
// Echo event details
}
}foreach($attending['employee']){
$empid = $attending['employee'];
$query = msqli_query($con, "SELECT * FROM employees WHERE employee_id = '$empid'")or die(mysqli_error($con));
while($event_employees = mysqli_fetch_array($query)){
// Echo employee details
}
}foreach($attending['team']){
$teamid = $attending['team'];
$query = msqli_query($con, "SELECT * FROM club_teams WHERE clb_team_id = '$teamid'")or die(mysqli_error($con));
while($event_team = mysqli_fetch_array($query)){
// Echo team details
}
}
}
}
这种方法非常低效且浪费,因为它会检索重复数据(即:'attending_employees' table 中所有重复的“club_event_id”。)
试试这个:
$query = msqli_query(
$con,
"SELECT"
. " attending_employees.*"
. ", club_events.*"
. ", employees.*"
. ", club_teams.*"
. " FROM"
. " attending_employees"
. " LEFT JOIN club_events ON club_events.club_event_id = attending_employees.club_event"
. " LEFT JOIN employees ON employees.employee_id = attending_employees.employee"
. " LEFT JOIN club_teams ON club_teams.clb_team_id = attending_employees.team"
) or die(mysqli_error($con));
我有三个 table:
我想显示 'Event Details',它显示参加员工的详细信息(来自 'attending_employees table > 相应员工详细信息表格的员工 ID 列表 'employee' table),他们属于哪个团队(来自 club_teams table)以及来自 'club_events' table 的活动详情)。
目前我正在使用多个 mysqli 查询来显示此信息,但是无法在一个查询中从数据库中提取数据(即:LEFT JOIN)。非常感谢您的帮助!
以下是我目前正在使用的查询:
$query = msqli_query($con, "SELECT * FROM attending_employees")or die(mysqli_error($con));
if(mysqli_num_rows($query) > 0){
while($attending = mysqli_fetch_array($query)){
foreach($attending['club_event']){
$eventid = $attending['club_event'];
$query = msqli_query($con, "SELECT * FROM club_events WHERE club_event_id = '$eventid'")or die(mysqli_error($con));
while($event_details = mysqli_fetch_array($query)){
// Echo event details
}
}foreach($attending['employee']){
$empid = $attending['employee'];
$query = msqli_query($con, "SELECT * FROM employees WHERE employee_id = '$empid'")or die(mysqli_error($con));
while($event_employees = mysqli_fetch_array($query)){
// Echo employee details
}
}foreach($attending['team']){
$teamid = $attending['team'];
$query = msqli_query($con, "SELECT * FROM club_teams WHERE clb_team_id = '$teamid'")or die(mysqli_error($con));
while($event_team = mysqli_fetch_array($query)){
// Echo team details
}
}
}
}
这种方法非常低效且浪费,因为它会检索重复数据(即:'attending_employees' table 中所有重复的“club_event_id”。)
试试这个:
$query = msqli_query(
$con,
"SELECT"
. " attending_employees.*"
. ", club_events.*"
. ", employees.*"
. ", club_teams.*"
. " FROM"
. " attending_employees"
. " LEFT JOIN club_events ON club_events.club_event_id = attending_employees.club_event"
. " LEFT JOIN employees ON employees.employee_id = attending_employees.employee"
. " LEFT JOIN club_teams ON club_teams.clb_team_id = attending_employees.team"
) or die(mysqli_error($con));