简体   繁体   中英

How to write a mysql query to return result comparing rows of same table?

I have a table structure as shown in following image. Basically, it has users ids lke u1, u2 who will be giving exams like E1, E2 etc on given date at a given exam center/venue in given shift like shift 1, shift 2 etc. There will be maximum 4 shifts in a day with shifts nos. from 1 to 4.

Given this table, I need to find out rows where same user is giving exam on same date and center but in continuous shifts. eg Red rows highlighed in image where U1 is giving two exams in same center but in continuous shifts ie 1 and 2. It can happen that user is giving at max 4 exams in one day in 4 consecutive shifts.

Another query i am trying to write is where, if the same user is giving two or more exams on same date but in different centers. Rows highlighted in blue.

在此处输入图片说明

Please try...

SELECT tblExams.srNum AS srNum,
       tblExams.user_id AS user_id,
       tblExams.exam_id AS exam_id,
       tblExams.exam_date AS exam_date,
       tblExams.exam_center_id AS exam_center_id,
       tblExams.exam_shift as exam_shift
FROM tblExams
JOIN
{
    SELECT user_id AS user_id,
           exam_date AS exam_date,
           exam_center_id AS exam_center_id,
           COUNT( exam_center_id ) AS exam_center_id_count
    FROM tblExams
    GROUP BY user_id,
             exam_date,
             exam_center_id
} exam_center_counter ON tblExams.user_id = exam_center_counter.user_id
                     AND tblExams.exam_date = exam_center_counter.exam_date
                     AND tblExams.exam_center_id = exam_center_counter.exam_center_id
WHERE exam_center_counter.exam_center_id_count >= 2;

The way I have interpreted your question you are asking for records where the user has 2 or more exams in the same center on the same date . This suggested to me that the User was the most dominant factor, and within that the Exam Date , and within that the Centers and the count of Exams . Thus the GROUP BY and COUNT() lines from my inner SELECT statement.

The other three selected fields are there partly because the GROUP BY uses them and thus requires them to be part of the SELECT and partly because they are needed to form the INNER JOIN with tblExams (the name that I have assumed for your table of data). (Note : Where the word JOIN is not preceded by a join type then an INNER JOIN is performed).

The INNER JOIN has the effect here of tacking on the count of exams taking place in that Exam Center on that Date for that User onto the corresponding row(s).

Then all we need to do is select all the fields from each row from tblExams where that count is at least 2 .

A variation on this logic was used when I constructed the following code for your second query...

SELECT tblExams.srNum AS srNum,
       tblExams.user_id AS user_id,
       tblExams.exam_id AS exam_id,
       tblExams.exam_date AS exam_date,
       tblExams.exam_center_id AS exam_center_id,
       tblExams.exam_shift as exam_shift
FROM tblExams
JOIN
{
    SELECT user_id AS user_id,
           exam_date AS exam_date,
           COUNT( exam_center_id ) AS exam_center_count
    FROM
    {
        SELECT user_id AS user_id,
               exam_date AS exam_date,
               exam_center_id AS exam_center_id
        FROM tblExams
        GROUP BY user_id,
                 exam_date,
                 exam_center_id
    } exam_center_id_grouper
    GROUP BY user_id,
             exam_date
} exam_center_counter ON tblExams.user_id = exam_center_counter.user_id
                     AND tblExams.exam_date = exam_center_counter.exam_date
WHERE exam_center_counter.exam_center_count >= 2;

In this query I use the inner-most SELECT statement to get a list of Exam Centers that are attended by each User on various Dates .

This data is then used by the middle-most SELECT statement to form a list of how many Centers that each User attends for an Exam on each DATE they do so.

This count is used by the outer-most SELECT to return only those rows from tblExams that meet the criteria specified.

If you have any questions or comments, then please feel free to post a Comment accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM