I have been playing around a bit and tried to find a solution but getting a bit stuck.
I have a 'meetings' table with 2 columns holding an id from 'members' which I need the members details to show on my meeting list.
I tried this, but it does not work...
$q = "SELECT (meetings.id) AS meetings_id, meetings.columnA, meetings.ColumnB, (members.id) AS members_id, members.first, members.last FROM meetings **LEFT JOIN members ON meetings.columnA = members.id AND meetings.columnB = members.id**";
$r = mysqli_query($dbc, $q);
while($row = mysqli_fetch_assoc($r)) { }
I can only get it to work if I only join on a single value, either 'columnA' or columnB', but this obviously only retrieved the data of for a single member.
Though this way works but is not ideal...
$q = "SELECT * FROM meetings";
$r = mysqli_query($dbc, $q);
while($row = mysqli_fetch_assoc($r)) {
$qm = "SELECT * FROM members WHERE id = '$row[columnA]'";
$rm = mysqli_query($dbc, $qm);
while($rowm = mysqli_fetch_assoc($rm)) {
if ($row['columnA'] == $rowm['id']) { echo $rowm['first']; }
}
$qm = "SELECT * FROM members WHERE id = '$row[columnB]'";
$rm = mysqli_query($dbc, $qm);
while($rowm = mysqli_fetch_assoc($rm)) {
if ($row['columnB'] == $rowm['id']) { echo $rowm['first']; }
}
}
Is there a way to get the JOIN to work or better way, so that I just need to make a single query?
Thanks.
, (members.id) AS members_id, members.first, members.lastfrom theSELECTsection of your query.LEFT JOINshould already do this. Granted, in my experience, using aJOINwould add all of the columns of that table into the query results. I don't know how to filter theJOINso that it doesn't select all of the columns.