我写了这段代码:
SELECT DISTINCT c.deptid Dept,
c.id Course,
t.startdate
FROM course c
LEFT JOIN section s
ON courseid = c.id
LEFT JOIN term t
ON termid = t.id
AND t.startdate < '2011-4-1'
GROUP BY course
HAVING t.startdate IS NULL
ORDER BY dept,
course;
我需要通过仅使用join或left join来删除group by和aggregate函数。左连接正在创建一个额外的空行,我不知道如何在没有group by的情况下解释它。任何有关重写的帮助都将非常感激。
编辑:此代码有效。这不是问题。问题是我不允许使用group by或任何聚合函数。
答案 0 :(得分:0)
我不确定这是否可以解决您的问题,但这是您的查询应该是什么样的:
SELECT c.deptId as Dept, c.id as Course, startDate
FROM Course c LEFT JOIN
Section s
ON courseId = c.id LEFT JOIN
Term t
ON termId = t.id AND startDate < '2011-04-01'
WHERE startDate IS NULL
GROUP BY course
ORDER BY Dept, Course;
以下是更改:
distinct
是不必要的,因为您正在执行group by
NULL
日期的比较已移至where
子句group by
语句使用课程as
引入(为了便于阅读)答案 1 :(得分:0)
您不应该有group by
子句,而having
子句应该是where
子句。试试这个:
SELECT DISTINCT c.deptid Dept,
c.id Course,
t.startdate
FROM course c
LEFT JOIN section s
ON courseid = c.id
LEFT JOIN term t
ON termid = t.id
AND t.startdate < '2011-04-01'
WHERE t.startdate IS NULL
ORDER BY dept,
course;
这将找到不有术语的所有课程选择。