我无法找到此SQL错误的解决方案

时间:2012-01-05 18:59:09

标签: php mysql

我有一个内部连接语句,我试图从两个表中收集数据。我的代码如下:

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

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

if (mysql_num_rows($result) == 0) {
       echo 'Nobody Is Attending This Meeting Yet';
    } else {

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

        echo "<tr>";
        echo "<td><br/>" .'Usernames: <br/><br/>'. $info['username']." </td>";
        echo '<br/><a href="viewattendees.php?meeting_id=' . $info['meeting_id'] . '">View Attendees</a>';

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

我得到的错误信息如下:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND attendees.meeting_id WHERE meeting_id = '31'' at line 1

我的数据库表如下所示:

会议:meeting_id,职称,主席,秘书,行动

与会者:attendees_id,meeting_id,用户名

我在与会者表中的内容是一些已确认参加会议的用户。会议信息在我的会议桌上。我有一个会议列表出现在我的网站上,如果点击会议,我希望所有参加会议的用户都显示在屏幕上。谁知道我怎么能这样做?

感谢

6 个答案:

答案 0 :(得分:4)

您的JOIN语法不正确,应该是这样的,

... INNER JOIN Meetings ON Meetings.meeting_id = attendees.meeting_id WHERE ...

答案 1 :(得分:3)

SELECT * FROM Attendees
INNER JOIN 
Meetings
ON Attendees.meeting_id =Meetings.meeting_id 
WHERE Meetings.meeting_id = '$id'

答案 2 :(得分:2)

给这个摆动:

SELECT * FROM attendees a
    INNER JOIN Meetings m 
        ON a.meeting_id = m.meeting_id
    WHERE a.meeting_id = '$id'

答案 3 :(得分:1)

这是你的问题:

INNER JOIN Meetings.meeting_id AND attendees.meeting_id 

我认为你的意思是:

INNER JOIN Meetings ON Meetings.meeting_id = attendees.meeting_id 

答案 4 :(得分:1)

尝试此查询内容..

select * from attendees a inner join meetings m 
on a.meeting_id=m.meeting_id where a.meeting_id='$id'"

答案 5 :(得分:1)

您的JOIN语法错误。 INNER JOIN应位于两个表名(attendees INNER JOIN Meetings)之间,后跟ON子句,该子句将第一个表中的列映射到第二个表中的一个(ON attendees.meeting_id = Meetings.meeting_id

SELECT * 
FROM
  attendees 
  INNER JOIN Meetings ON attendees.meeting_id = Meetings.meeting_id
WHERE attendees.meeting_id = '$id'