错误 #1066 - 在 MySQL 中不唯一 table/alias
Error #1066 - Not unique table/alias in MySQL
我的 SQL 语句有问题,出现错误
#1066 - Not unique table/alias: 'students'
下面是我的代码:
SELECT batches.*,students.*, courses.course_name AS course_name,
count(DISTINCT IF(attendances.student_id = students.id AND attendances.permitted = 0,attendances.id ,NULL)) AS absents_count,
count(DISTINCT IF(batches.id = students.batch_id ,students.id ,NULL)) AS students_count, count(DISTINCT IF(attendances.student_id = students.id AND attendances.permitted = 1,attendances.id ,NULL)) AS leaves_count
FROM `batches`
INNER JOIN courses ON courses.id = batches.course_id
LEFT OUTER JOIN attendances ON attendances.batch_id = batches.id AND attendances.month_date = '2016-09-05'
LEFT OUTER JOIN students ON students.id = attendances.student_id AND students.batch_id = batches.id
INNER JOIN students ON batches.id = students.batch_id
WHERE ('2016-09-05' BETWEEN batches.start_date AND batches.end_date AND batches.is_active = 1 AND batches.is_deleted = 0 AND courses.is_deleted = 0 )
GROUP BY batches.id
ORDER BY courses.course_name,batches.id
LIMIT 0, 10;
leaves_count 和 absents_count 工作正常但是当我为 students_count 添加代码时它给出了提到的错误。
您正在加入 table students
两次,并且在 select 和 where 列表中它不知道应该从哪个 table 获取数据。您需要在至少一个连接中为您的 table 添加别名,例如替换:
LEFT OUTER JOIN students ON
students.id = attendances.student_id AND students.batch_id = batches.id
INNER JOIN students ON
batches.id = students.batch_id
与:(注意我已经替换了两个只是为了向您展示这一点)
LEFT JOIN students AS s1 ON
s1.id = attendances.student_id AND s1.batch_id = batches.id
INNER JOIN students AS s2 ON
batches.id = s2.batch_id
您还需要将整个查询中从 students
到 s1
或 s2
的每个列引用替换为 retrieve/constraint 您的查询正确。
我还删除了左连接中的外部部分,因为它是多余的。
为 table students
和所有相关列使用不同的别名
select <Fields to be seleced>
FROM `batches`
INNER JOIN courses ON courses.id = batches.course_id
LEFT OUTER JOIN attendances ON attendances.batch_id = batches.id AND attendances.month_date = '2016-09-05'
LEFT OUTER JOIN students st1 ON st1.id = attendances.student_id AND students.batch_id = batches.id
INNER JOIN students st2 ON batches.id = st2.batch_id
WHERE ('2016-09-05' BETWEEN batches.start_date AND batches.end_date AND batches.is_active = 1 AND batches.is_deleted = 0 AND courses.is_deleted = 0 ) GROUP BY batches.id ORDER BY courses.course_name,batches.id LIMIT 0, 10;
我的 SQL 语句有问题,出现错误
#1066 - Not unique table/alias: 'students'
下面是我的代码:
SELECT batches.*,students.*, courses.course_name AS course_name,
count(DISTINCT IF(attendances.student_id = students.id AND attendances.permitted = 0,attendances.id ,NULL)) AS absents_count,
count(DISTINCT IF(batches.id = students.batch_id ,students.id ,NULL)) AS students_count, count(DISTINCT IF(attendances.student_id = students.id AND attendances.permitted = 1,attendances.id ,NULL)) AS leaves_count
FROM `batches`
INNER JOIN courses ON courses.id = batches.course_id
LEFT OUTER JOIN attendances ON attendances.batch_id = batches.id AND attendances.month_date = '2016-09-05'
LEFT OUTER JOIN students ON students.id = attendances.student_id AND students.batch_id = batches.id
INNER JOIN students ON batches.id = students.batch_id
WHERE ('2016-09-05' BETWEEN batches.start_date AND batches.end_date AND batches.is_active = 1 AND batches.is_deleted = 0 AND courses.is_deleted = 0 )
GROUP BY batches.id
ORDER BY courses.course_name,batches.id
LIMIT 0, 10;
leaves_count 和 absents_count 工作正常但是当我为 students_count 添加代码时它给出了提到的错误。
您正在加入 table students
两次,并且在 select 和 where 列表中它不知道应该从哪个 table 获取数据。您需要在至少一个连接中为您的 table 添加别名,例如替换:
LEFT OUTER JOIN students ON
students.id = attendances.student_id AND students.batch_id = batches.id
INNER JOIN students ON
batches.id = students.batch_id
与:(注意我已经替换了两个只是为了向您展示这一点)
LEFT JOIN students AS s1 ON
s1.id = attendances.student_id AND s1.batch_id = batches.id
INNER JOIN students AS s2 ON
batches.id = s2.batch_id
您还需要将整个查询中从 students
到 s1
或 s2
的每个列引用替换为 retrieve/constraint 您的查询正确。
我还删除了左连接中的外部部分,因为它是多余的。
为 table students
和所有相关列使用不同的别名
select <Fields to be seleced>
FROM `batches`
INNER JOIN courses ON courses.id = batches.course_id
LEFT OUTER JOIN attendances ON attendances.batch_id = batches.id AND attendances.month_date = '2016-09-05'
LEFT OUTER JOIN students st1 ON st1.id = attendances.student_id AND students.batch_id = batches.id
INNER JOIN students st2 ON batches.id = st2.batch_id
WHERE ('2016-09-05' BETWEEN batches.start_date AND batches.end_date AND batches.is_active = 1 AND batches.is_deleted = 0 AND courses.is_deleted = 0 ) GROUP BY batches.id ORDER BY courses.course_name,batches.id LIMIT 0, 10;