我对我想进行的SQL查询有疑问。假设我在表中有一列具有以下值的列:学校列:年级。
SUI grades | Score
2 9 2
2 9
5 4 1
5 4 1
5 4
6 1 1
6 1
和表Grade_scores
id score_1 score_2
1 4 1
现在,如果分数已满且 school.grade 为< strong>与 Grade_scores.score_1 相同或 Grade_scores.score_2 相同。所以我的输出将是:
SUI Count
5 2
6 1
到目前为止的代码...
SELECT SUI, Count(Grades)
FROM mytable
WHERE Score <> ''
GROUP BY SUI
答案 0 :(得分:0)
(未经测试):
SELECT SUI, count(GRADES) As Count
FROM school
INNER JOIN Gradescores ON s.grades = score_1 OR s.grades = score_2
WHERE Score>'' AND NOT Score IS NULL
GROUP BY SUI
答案 1 :(得分:0)
您需要JOIN
上的school
和grade_scores
表中的grades
和grade_scores
表位于SELECT s.SUI, COUNT(s.grades) AS `Count`
FROM grade_scores gs
JOIN school s ON s.Score IS NOT NULL AND s.grades IN (gs.score_1, gs.score_2)
GROUP BY s.SUI
中的两个值中:
SUI Count
5 2
6 1
输出:
Score
注意:该查询假设NULL
列中的空值为s.Score IS NOT NULL
。如果它们是空字符串,请将s.Score != ''
替换为sum
答案 2 :(得分:0)
我更喜欢使用exists
进行此操作,因为如果join
/ score_1
中存在重复的值,则score_2
方法将使计数加倍:
select s.sui, count(*)
from scores s
where s.score is not null and
s.score in (select 1
from grade_scores gs
where s.grades in (gs.score_1, gs.score_2)
)
group by s.sui;