如何编写一个mysql查询来返回比较相同表的行的结果?

时间:2017-04-08 04:08:59

标签: mysql

我有一个表结构,如下图所示。基本上,它有用户ID,如果在给定的考试中心/场地,在给定的班次,例如班次1,轮班2等,将在给定日期给予E1,E2等考试,一天最多4班。与转移号码。从1到4。

鉴于此表,我需要查找相同用户在相同日期和中心进行检查的行,但是要连续轮班。例如,在图像中高亮的红色行,其中U1在相同的中心进行两次检查但是连续移位,即1和2.可能发生用户在连续4次轮班中在一天内进行最多4次检查。

我想写的另一个问题是,如果同一个用户在相同的日期但是在不同的中心提供两个或更多的考试。行以蓝色突出显示。

enter image description here

1 个答案:

答案 0 :(得分:1)

请尝试......

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;

我解释您的问题的方式是您要求user 2 or more exams same center the same date中的User Exam Date。这向我建议Centers是最主要的因素,count内,ExamsGROUP BY COUNT()。因此,我的内部SELECT语句中的GROUP BYSELECT行。

其他三个选定的字段部分是因为INNER JOIN使用了它们,因此要求它们成为tblExams的一部分,部分原因是需要它们与JOIN形成INNER JOIN {1}}(我为您的数据表假设的名称)。 (注意:如果单词INNER JOIN前面没有连接类型,则会执行Exam Center

此处Date的效果是将User tblExams2的{​​{1}}考试计数添加到相应的行上

然后,我们需要做的就是从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; 的每一行中选择所有字段,其中该计数至少为Exam Centers

当我为您的第二个查询构建以下代码时,使用了此逻辑的变体...

User

在此查询中,我使用最内层的SELECT语句来获取Dates上每个SELECT所出席的Centers列表。

然后,最中间的User语句会使用此数据,以形成每个Exam DATE在每个SELECTtblExams //Initialize the model matrix for the current transformation Matrix.setIdentityM(mModelMatrixCurrent, 0); //Apply the current transformations Matrix.translateM(mModelMatrixCurrent, 0, cameraX, cameraY, cameraZ); Matrix.rotateM(mModelMatrixCurrent, 0, mAngle, 0.0f, 0.0f, 1.0f); //Multiply the accumulated transformations by the current transformations Matrix.multiplyMM(mTempMatrix, 0, mModelMatrixCurrent, 0, mModelMatrixAccumulated, 0); System.arraycopy(mTempMatrix, 0, mModelMatrixAccumulated, 0, 16); 的列表Start-Job他们这样做了。

最外层Start-Job使用此计数仅返回符合指定条件的$null行。

如果您有任何问题或意见,请随时发表评论。