Reputation: 65
I am getting confused with this. I have searched the internet and it is still confusing me. I have the following tables:
Meetings: meeting_id
, title
, chairman
, secretary
, occurances
Rooms: room_id
, room
, date
, time
, meeting_id
I want everything to be displayed on a table. My PHP is as follows:
<?php
$result = mysql_query("SELECT * FROM Meetings INNER JOIN Rooms ON meeting_id = Rooms.meeting_id")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Meetings Setup Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>Title</th><th>Chairperson</th><th>Secretary</th><th>Terms of Reference</th><th>Occurances</th><th>Room</th><th>Date</th><th>Time</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><br/>" . $info['title']." </td>";
echo "<td><br/>" . $info['chairperson']. "</td>";
echo "<td><br/>" . $info['secretary']."</td>";
echo "<td><br/>" . $info['tof']. "</td>";
echo "<td><br/>" . $info['occurances']. "</td>";
echo "<td><br/>" . $info['room']. "</td>";
echo "<td><br/>" . $info['date']. "</td>";
echo "<td><br/>" . $info['time']. "</td>";
}
}
echo "</tr>";
echo "</table>";
?>
It comes out with the following error message:
Column 'meeting_id' in on clause is ambiguous
I am trying to display all the fields from the meetings table and obviously the room, date and time only.
How do I fix this?
Upvotes: 0
Views: 391
Reputation: 19027
mysql_query("SELECT * FROM Meetings INNER JOIN Rooms ON Meetings.meeting_id = Rooms.meeting_id")
When column names are same on join
columns, you must explicitly mention those columns belonging to a specific table.
Upvotes: 0
Reputation: 2733
The error message states the meeting_id is ambiguous so you need to specify which table you want to constraint as meeting_id.
SELECT * FROM Meetings INNER JOIN Rooms ON Meetings.meeting_id = Rooms.meeting_id
Upvotes: 0
Reputation: 1780
You need to define which table you want meeting_id
to be selected from.
e.g.
`SELECT * FROM Meetings INNER JOIN Rooms ON Meetings.meeting_id = Rooms.meeting_id`
Upvotes: 1
Reputation: 16708
You need to prefix both columns in the join with their table names, i.e.
FROM Meetings INNER JOIN Rooms ON Meetings.meeting_id = Rooms.meeting_id
The error message explains the problem quite clearly, actually: whenever you specify a column whose name exists in multiple tables, you must be explicit about which table's column you mean.
Upvotes: 2