我应该使用LEFT JOIN或INNER JOIN或RIGHT JOIN将3个不同的查询加入到单个查询中

时间:2011-12-23 05:32:57

标签: php mysql

第一次查询

$sql="SELECT distinct(syllabus.syllabusName) as subjectName,$cid as courseId, stud.batchId as batchId,course.courseId,course.syllabusId AS subjectID,course.unitId,stud.studentID as studentID  ,concat(stud.studentFirstName,' ',stud.studentLastName,' ',stud.studentRegistrationNumber) AS studentName
            FROM studentdetails  stud           
            INNER JOIN  `coursemapping` course 
            ON  stud.`courseId`=course.courseId
            INNER JOIN syllabus syllabus
            ON  course.syllabusId=syllabus.syllabusId 
            WHERE course.courseId ='$cid' AND course.syllabusId='$sid' AND stud.`batchId`='$bid'";  

第二次查询

     $sql=" SELECT A.assignmentId, A.unitId, B.assignmentName, C.assignmentsBatchId AS batchId, C.courseId AS courseId,C.assignmentMappingId, D.studentId, concat( E.studentFirstName, ' ', E.studentLastName, ' ', E.studentRegistrationNumber ) AS studentName
            FROM studentdetails E
            INNER JOIN assignmentscompleted D ON E.studentId = D.studentId
            INNER JOIN assignmentsbatch C ON D.assignmentId = C.assignmentMappingId
            INNER JOIN assignments B ON D.assignmentId = B.assignmentId
            INNER JOIN assignmentsMapping A ON B.assignmentId = A.assignmentId
            WHERE A.assignmentId = '$sid'
            AND C.assignmentsBatchId = '$bid'
            AND C.courseId ='$cid'";

第三次查询

    $sql    =   "SELECT distinct(C.syllabusName) as subjectName,B.maxAccadamicMarks as marksScored,B.academicMarks as maxMarks,B.marksPercentage as percentage  
                    FROM studentdetails A INNER JOIN marks B 
                    ON A.studentId=B.studentId AND A.courseId=B.courseId AND A.batchId=B.batchId 
                    INNER JOIN syllabus C 
                    ON  B.syllabusId=C.syllabusId WHERE A.studentUserName='$studentUserName'";

这里我需要加入所有三个查询才能获取主题,学生,评分,分数,百分比?如何将这三个查询连接到单个查询

1 个答案:

答案 0 :(得分:0)

看起来您可能很容易将查询合并在一起,因为许多人使用公共表,但是如果没有表结构,我只是编写一个快速而脏的mashup或您提供的三个查询。我假设这些查询中的每一个都准确地返回了您所需要的内容而没有其他内容。

我稍微改变了第三个查询,以便在studentID中添加,以便它可以与其他查询正确连接,并在第二个查询中注释掉冗余信息。

select
    a.studentID as studentID,
    a.studentName as studentName,
    $cid as courseID,
    a.batchID as batchID,
    a.subjectID as subjectID,
    c.subjectName as subjectName,
    a.unitID as unitID,
    b.assignmentID as assignmentID,
    b.assignmentName as assignmentName,
    b.assignmentMappingID as assignmentMappingID,
    c.marksScored as marksScored,
    c.maxMarks as maxMarks,
    c.percentage as percentageMarks
from
(
    select 
        distinct(syllabus.syllabusName) as subjectName,
        $cid as courseID, 
        stud.batchId as batchID,
        course.courseID,
        course.syllabusId as subjectID,
        course.unitID,
        stud.studentID as studentID  ,
        concat(stud.studentFirstName,' ',stud.studentLastName,' ',stud.studentRegistrationNumber) AS studentName
    FROM 
        studentdetails  stud           
            INNER JOIN  `coursemapping` course 
                ON  stud.`courseId`=course.courseId
            INNER JOIN syllabus syllabus
                ON  course.syllabusId=syllabus.syllabusId 
    WHERE 
        course.courseId ='$cid' 
        AND course.syllabusId='$sid' 
        AND stud.`batchId`='$bid'
) a,
(
    SELECT 
        A.assignmentID, 
        A.unitID, 
        B.assignmentName, 
        C.assignmentsBatchId AS batchID, 
        C.courseId AS courseID,
        C.assignmentMappingID, 
        D.studentID, 
        --concat( E.studentFirstName, ' ', E.studentLastName, ' ', E.studentRegistrationNumber ) AS studentName
    FROM 
        studentdetails E
            INNER JOIN assignmentscompleted D 
                ON E.studentId = D.studentId
            INNER JOIN assignmentsbatch C 
                ON D.assignmentId = C.assignmentMappingId
            INNER JOIN assignments B 
                ON D.assignmentId = B.assignmentId
            INNER JOIN assignmentsMapping A 
                ON B.assignmentId = A.assignmentId
    WHERE 
        A.assignmentId = '$sid'
        AND C.assignmentsBatchId = '$bid'
        AND C.courseId ='$cid'
) b,
(
    SELECT 
        distinct(C.syllabusName) as subjectName,
        B.maxAccadamicMarks as marksScored,
        B.academicMarks as maxMarks,
        B.marksPercentage as percentage,
        A.studentID,
        a.courseID as courseID
    FROM 
        studentdetails A 
            INNER JOIN marks B 
                ON A.studentId=B.studentId 
                AND A.courseId=B.courseId 
                AND A.batchId=B.batchId 
            INNER JOIN syllabus C 
                ON  B.syllabusId=C.syllabusId 
    WHERE 
        A.studentUserName='$studentUserName'
) c
where
    a.studentID=b.studentID
    and a.studentID=c.studentID
    and a.courseID=b.courseID
    and a.courseID=c.courseID

工作而不会在单个查询中获得笛卡尔联接,但如果没有表结构,则很难确定

说了这么多之后,我完全赞同上面的一些评论,通过合并所有这些查询,你将获得一个记录集,它将在每行上反复重复某些信息,对你来说是无用的。最好运行一个查询,将值弹出到变量中,然后根据需要使用它们。