[英]need help building one mysql query comparing a variable with two tables
I have built 2 queries that compares the $project_id
variable to the 2 tables MEETING
& MEETING_AGENDA
shown in the flow diagram below: 我建立了2个查询,将
$project_id
变量与MEETING_AGENDA
所示的2个表MEETING
& MEETING_AGENDA
进行比较:
+---------------------+
+------------+ | MEETING_AGENDA |
| MEETING | +---------------------+
+------------+ | meeting_agenda_id |
| meeting_id |----->| meeting_id |
$project_id --->| project_id | | meeting_agenda_name |
+------------+ +---------------------+
The logic behind this flow diagram is, I have stored a variable called $project_id
which acts as the input. 该流程图背后的逻辑是,我存储了一个名为
$project_id
的变量,该变量充当输入。 If the $project_id = MEETING.project_id
then I need to store a list of the MEETING.meeting_id
's. 如果
$project_id = MEETING.project_id
那么我需要存储MEETING.meeting_id
的列表。 There are multiple duplicates of project_id
's in MEETING
resulting in an array of meeting_id
's (just for clarification). MEETING
存在project_id
的多个重复项,从而产生了一系列meeting_id
的(仅作澄清)。 If any of the MEETING.meeting_id
's = MEETING_AGENDA.meeting_id
then print meeting_agenda_name
. 如果有任何的
MEETING.meeting_id
的= MEETING_AGENDA.meeting_id
然后打印meeting_agenda_name
。
My messy attempt (which works) looks like this: 我的混乱尝试(有效)如下所示:
$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>"
}
}
I use 2 query's, I would like to clean this up into 1 query if possible. 我使用2个查询,如果可能的话,我想将其清理为1个查询。 I have tried varies attempts at a single query but nothing has worked for me.
我在单个查询中尝试了各种尝试,但没有任何帮助。 Here is my latest attempt.
这是我最近的尝试。
NEW QUERY: 新查询:
$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
";
I apologize for my lack of mysql knowledge, but any help is appreciated. 对于缺乏mysql知识,我深表歉意,但对您的任何帮助,我们深表感谢。
You can get that information in a single query with the following: 您可以通过以下查询在单个查询中获得该信息:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.