user1114080
user1114080

Reputation: 65

Joining 3 Tables In MySQL

I am trying to join three tables together without any success so far. I managed to join two tables together and I am extracting the right data out but as soon as i add in a few more words to my sql statement, no data is being displayed. the code is as follows:

    <?php
$id = $_GET['meeting_id'];

$result = mysql_query("SELECT * FROM Meetings INNER JOIN Minutes ON Meetings.meeting_id = Minutes.meeting_id INNER JOIN attendees ON attendees.meeting_id AND attendees.username WHERE Minutes.approval = 'approved' AND Meetings.meeting_id = '$id'")
or die(mysql_error());

if (mysql_num_rows($result) == 0) {
       echo '<h3>There Arent Any Minutes For This Meeting Yet</h3>';
    } else {

while($info = mysql_fetch_array($result))
{

        echo "<tr>";
        echo "<td><br/>" .'Title: '. $info['title']." </td>";
        echo "<td><br/><br/>" .'Subject: '. $info['subject']. "</td>";
        echo "<td><br/><br/>" .'Next Subject: '. $info['next_subject']."</td>";
        echo "<td><br/><br/>" .'Attendees: '. $info['username']."</td>";


        }
    }
echo "</tr>";
echo "</table>";


?>

my database tables are as follows:

Meetings: meeting_id, title, subject, chairman, secretary, action

Minutes: minute_id, subject, next_subject, approval, meeting_id

attendees: attendees_id, meeting_id, username

anyone....?

Upvotes: 0

Views: 83

Answers (2)

Dave Barker
Dave Barker

Reputation: 6437

The attendees table isn't being joined correctly.

SELECT * 
  FROM Meetings 
 INNER JOIN Minutes ON Meetings.meeting_id = Minutes.meeting_id 
 INNER JOIN attendees ON Meetings.meeting_id = attendees.meeting_id 
 WHERE Minutes.approval = 'approved' 
   AND Meetings.meeting_id = '$id'

Your SQL will only return meetings that have attendees and minutes. If a meeting doesn't have these then change the INNER JOIN statements to LEFT JOIN.

Upvotes: 3

Shyju
Shyju

Reputation: 218722

SELECT Meetings.Meeting_Id,Meetings.Title,Meetings.Subject,
Minutes.Subject AS MinuteSubject,
Attendees.AttendeesID
FROM
Meetings
INNER JOIN
Minutes
ON Meetings.Meeting_id=Minutes.Meeting_Id
INNER JOIN
Attendees
ON Attendees.Meeting_id=Meetings.Meeting_ID
WHERE Meetings.Meeting_id=24

You can replace 25 with your variable

Upvotes: 2

Related Questions