需要帮助构建一个mysql查询,比较一个变量和两个表

时间:2013-11-25 18:49:16

标签: php mysql

我构建了2个查询,将$project_id变量与2个表MEETING& MEETING_AGENDA显示在下面的流程图中:

                                    +---------------------+
                +------------+      |    MEETING_AGENDA   |
                |  MEETING   |      +---------------------+
                +------------+      | meeting_agenda_id   |
                | meeting_id |----->| meeting_id          |
$project_id --->| project_id |      | meeting_agenda_name |
                +------------+      +---------------------+

这个流程图背后的逻辑是,我存储了一个名为$project_id的变量作为输入。如果是$project_id = MEETING.project_id,那么我需要存储MEETING.meeting_id的列表。 project_id中有多个MEETING重复项,导致meeting_id数组(仅供澄清)。如果MEETING.meeting_id = MEETING_AGENDA.meeting_id中的任何一个meeting_agenda_name,则打印$project_id = $_SESSION['PROJECT_ID']; $query1 = " SELECT meeting_id, meeting_project_id FROM MEETING WHERE project_id = $project_id "; $result1 = mysqli_query($con, $query1) or die("Query error: " . mysqli_error($con)); while($row = mysqli_fetch_array($result1)){ $meeting_ids = $row['meeting_id']; $query2 = " SELECT * FROM MEEITNG_AGENDA WHERE meeting_id = $meeting_ids "; $result2 = mysqli_query($con, $query2) or die("Query error: " . mysqli_error($con)); while($row2 = mysqli_fetch_array($result2)){ echo $row2['meeting_agenda_name'] . "<br>" } }

我的混乱尝试(有效)看起来像这样:

 $query = 
 "
 SELECT MEETING.project_id, MEETING.meeting_id, MEETING_AGENDA.*
 FROM MEETING
 WHERE MEETING.project_id = $project_id
 INNER JOIN MEETING.project_id
 ON $project_id = MEETING.project_id
 ";

我使用2个查询,如果可能,我想将其清理为1个查询。我在单个查询中尝试了不同的尝试,但没有任何对我有用。这是我最近的尝试。

NEW QUERY:

{{1}}

我为我缺乏mysql知识而道歉,但感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容在单个查询中获取该信息:

SELECT m.project_id, m.meeting_id, ma.*
FROM MEETING m
INNER JOIN MEETING_AGENDA ma ON ma.meeting_id  = m.meeting_id 
WHERE m.project_id = $project_id