Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 左连接工作不正常_Mysql_Sql - Fatal编程技术网

Mysql 左连接工作不正常

Mysql 左连接工作不正常,mysql,sql,Mysql,Sql,表tbl_学生 Student_pid name email 1 waheed waheed@gmail.com 2 fareed fareed@gmail.com 表r_工作邀请 id_job email 101 waheed@gmail.com 101 fareed@gmail.com 123 wahe

表tbl_学生

Student_pid     name                email
1               waheed               waheed@gmail.com
2               fareed               fareed@gmail.com
表r_工作邀请

id_job   email
101       waheed@gmail.com
101       fareed@gmail.com
123       waheed@gmail.com
123       fareed@gmail.com
表r_工作组

student_id   job_id   group_id
1             101       1
2             101       2
1             123       1
2             123       2
从以上三个表中,我试图让学生们有一个条件。这是我的问题:

    $studentQuery = $conn->query("SELECT
      s.student_pid,jbi.test_status
       FROM `r_job_groups` jtg 
        LEFT JOIN tbl_students s ON jtg.student_id=s.student_pid 
        LEFT JOIN r_job_invitations jbi ON jbi.email=s.student_email 
         where jtg.group_id=".$group." and job_id=".$jobID );
从上面的查询中,查找

$group = 1 and $jobID = 101
结果如下:

student_pid
1
1
2
2
student_pid
    1
    2
实际结果应如下所示:

student_pid
1
1
2
2
student_pid
    1
    2
我的问题是我让学生们有更多的时间

根据查询,该结果应为2名学生,但由于工作id无法正常工作,结果为4名学生。

如何解决此问题?

在选择中使用变量时要小心,否则可能会被sql注入所替代

无论如何,您可以使用distinct来避免重复的值

  $studentQuery = $conn->query("SELECT DISCINCT
  s.student_pid
   FROM `r_job_groups` jtg 
    LEFT JOIN tbl_students s ON jtg.student_id=s.student_pid 
    LEFT JOIN r_job_invitations jbi ON jbi.email=s.student_email 
     where jtg.group_id=".$group." and jtg job_id=".$jobID );
或者使用一个独特的dinamic表

  $studentQuery = $conn->query("SELECT 
  s.student_pid
   FROM `r_job_groups` jtg 
    LEFT JOIN ( select distinct student_pid 
           from tbl_students )  s ON jtg.student_id=s.student_pid 
    LEFT JOIN r_job_invitations jbi ON jbi.email=s.student_email 
     where jtg.group_id=".$group." and jtg.job_id=".$jobID );
根据您的数据示例,还可以尝试更改joi表的顺序

$studentQuery = $conn->query("SELECT DISTINCT s.student_pid
      FROM  tbl_students s  
      LEFT `r_job_groups` jtg  s ON jtg.student_id=s.student_pid 
      LEFT JOIN r_job_invitations jbi ON jbi.email=s.student_email 
     where jtg.group_id=".$group." and job_id=".$jobID );

邀请函表似乎完全没有必要——这也是重复的原因。将查询编写为:

SELECT s.student_pid
FROM `r_job_groups` jtg LEFT JOIN
      tbl_students s
      ON jtg.student_id = s.student_pid 
WHERE jtg.group_id = ".$group." and jtg.job_id = ".$jobID;

我还怀疑您想要加入,而不是左加入,这是工作id的nme表。。。在结果而不是where条件下应用distincnt的任何方式我都不清楚为什么不使用distinct。。根据您的结果,您应该使用该子句,因为(我重复我自己)该子句应用于结果,而不涉及行过滤器(where子句)@AbdulWaheed。我已经更新了答案。。让我知道你能看到我更新的问题吗?我还需要另一个字段,因为我想它对我来说很混乱。
s.student\u pid,jbi.test\u status
我需要邀请表检查更新的问题一次@戈登
    SELECT jbi.*, s.student_pid,jbi.test_status FROM 
`r_job_groups` jtg LEFT JOIN tbl_students s ON jtg.student_id=s.student_pid
 LEFT JOIN r_job_invitations jbi ON jbi.email=s.student_email
 where jtg.group_id=1 and jtg.job_id=109 and jtg.job_id=jbi.id_job