Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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 - Fatal编程技术网

具有不同计数的mySQL多连接

具有不同计数的mySQL多连接,mysql,Mysql,我正在尝试查询3个表: 教室: id | name ---------------- 1 | bob's class studentId | classroomId ----------------------- 1 | 1 studentId | dateTime --------------------- 1 | 2014-08-15 09:33:23 学生注册: id | name ------------

我正在尝试查询3个表:

教室

id |    name  
----------------
 1 | bob's class
studentId | classroomId  
-----------------------
     1    |     1  
studentId | dateTime 
---------------------   
    1     | 2014-08-15 09:33:23
学生注册:

id |    name  
----------------
 1 | bob's class
studentId | classroomId  
-----------------------
     1    |     1  
studentId | dateTime 
---------------------   
    1     | 2014-08-15 09:33:23
学生进步:

id |    name  
----------------
 1 | bob's class
studentId | classroomId  
-----------------------
     1    |     1  
studentId | dateTime 
---------------------   
    1     | 2014-08-15 09:33:23
我试图得出这样的结果:

classroomName | studentId | days
------------------------------------------
Bob's class   |     1     |   112
其中,
days
是特定学生拥有
dateTime
条目的不同天数

SELECT classroom.name, student_enrollment.studentId, student_progress.dateTime   
FROM classroom 
JOIN student_enrollment ON student_enrollment.classroomId = classroom.id     
JOIN student_progress ON student_progress.studentId = student_enrollment.studentId 
WHERE `dateTime` >= '2014-08-01 09:00:34' 
ORDER BY classroom.name
为每个学生提供一个包含每个
dateTime
条目的表。我尝试向查询中添加计数的任何方法最多只能得到一个结果,即一个学生和distinct
dateTime
days的总计数

SELECT classroom.name, student_enrollment.studentId, COUNT(DISTINCT YEAR(student_progress.dateTime),MONTH(student_progress.dateTime),DAY(student_progress.dateTime)) AS days 
FROM classroom 
    JOIN student_enrollment ON student_enrollment.classroomId = classroom.id 
    JOIN student_progress ON student_progress.studentId = student_enrollment.studentId 
WHERE  `dateTime` >= '2014-08-01 09:00:34' 
ORDER BY classroom.name

我尝试将计数移动到联接部分中的子查询,以及其他各种事情,但是最终会出现错误,例如
未知表student\u progress
,并且似乎无法正确地将查询放在一起,以获得每个
studentId

日期时间
项的不同计数。似乎您想要的是计算不同的日期。请尝试:

SELECT classroom.NAME,
    student_enrollment.studentId,
    COUNT(DISTINCT DATE(student_progress.DATETIME)) AS days
FROM classroom
INNER JOIN student_enrollment
    ON student_enrollment.classroomId = classroom.id
INNER JOIN student_progress
    ON student_progress.studentId = student_enrollment.studentId
WHERE `dateTime` >= '2014-08-01 09:00:34'
GROUP BY classroom.NAME, student_enrollment.studentId,
ORDER BY classroom.NAME

我在这里也添加了显式分组BY,这是因为无论何时使用公式进行聚合,都应该按其他字段分组。MySQL无论如何都会这样做,但最好是显式的,这样你就不会得到奇怪的结果。

我想你需要这样的东西:

SELECT
    classroom.name,
    student_progress.studentId,
    COUNT(DISTINCT DATE_FORMAT(student_progress.dateTime, '%y-%m-%d')) AS days
FROM classroom
JOIN student_enrollment ON
    student_enrollment.classroomId = classroom.id
JOIN student_progress ON
    student_progress.studentId = student_enrollment.studentId
GROUP BY
    classroom.name,
    student_progress.studentId;

这应该正确计算不同的天数和不同的
dateTime
值。我不建议给你的专栏命名
dateTime
btw,这势必会导致以后的混乱(对于你或下一个开发人员)

是你想考虑的复杂性,而不是唯一的代码<代码> DATETIME/<代码>值?复杂的是,我刚刚在1个教室中得到1个学生的结果,以“天”作为所有不同日期时间的计数,而不是每一个特定学生的天数。我猜是因为我在选择中有student_enrollment.studentId而不是student_progress.studentId。啊,非常接近我发布的内容。这不会考虑几天,但是它会吗?谢谢你的回答,以及关于分组的提示。除了在08 / 01 / 2014之前没有过滤日期。我添加了WHERE
dateTime
='2014-08-01 09:00:34',它仍然返回该日期之前的学生、教室和条目计数。我想我需要以某种方式将这种逻辑放在计数中,而不是放在BYNevermind小组之前,看起来没有过滤的日期只是学生在student_注册表中多次出现的一种怪癖,它显示了他们今年在当前班级和上一年班级的天数计数。我应该能想出如何解决这个问题。