1

我有一张桌子,里面装满了学生提交的不同项目。每个项目应由三位不同的评委评判。所以还有另一个分数表,其中包括 projectid 和 Judgeid。我正在尝试编写一个查询,可以选择任何尚未判断三遍的项目。

这是一些示例表...

项目

projectid | projectname
-----------------------
9         | Kintetics
11        | Volcano
15        | Robots
22        | Oceanlife

分数

scoreid (AI) | projectid | judgeid | score
------------------------------------------
1            | 9         | 15      | 5
2            | 11        | 15      | 18
3            | 9         | 23      | 12
4            | 11        | 23      | 11
5            | 9         | 45      | 19
6            | 22        | 15      | 3

我希望此查询返回项目 11、15 和 22,因为这三个项目还没有分配给他们的三名评委。

4

5 回答 5

4
SELECT  a.ProjectID, a.ProjectName
FROM    Projects a
        LEFT JOIN Scores b
            ON a.ProjectID = b.ProjectID
GROUP   BY a.ProjectID, a.ProjectName
HAVING  COUNT(a.ProjectID) < 3

要进一步了解有关联接的更多信息,请访问以下链接:

输出

╔═══════════╦═════════════╗
║ PROJECTID ║ PROJECTNAME ║
╠═══════════╬═════════════╣
║        11 ║ Volcano     ║
║        15 ║ Robots      ║
║        22 ║ Oceanlife   ║
╚═══════════╩═════════════╝
于 2013-03-22T14:19:53.953 回答
1

另一种选择:

SELECT  a.ProjectID, a.ProjectName
FROM    Projects a
LEFT JOIN (select ProjectID, count(*) counted from Scores) b
       ON a.ProjectID = b.ProjectID
WHERE COALESCE(b.counted,0) < 3
于 2013-03-22T14:25:13.653 回答
1
SELECT COUNT(scores.id) AS score_count, projects.* 
FROM scores, projects
WHERE scores.projectid = projects.id
GROUP BY projects.id
HAVING score_count > 2
于 2013-03-22T14:26:38.403 回答
0

试试这个

select * from scores where id not in (select id from scores where project id in (select projectid from scores group by projectid having count(prjectid)=3))
于 2013-03-22T14:20:48.730 回答
0

试试这个:

SELECT * FROM Projects p WHERE (SELECT COUNT(*) FROM Scores s WHERE s.projectid = p.projectid) < 3
于 2013-03-22T14:22:21.373 回答