我想找到只有subjectID(0,1,2)的batchID,请帮助我,提前谢谢
我需要回答BatchID = 12和BatchID = 51,我该怎么办sql
MyTable的
uid BatchID SubjectID
6 2 0
7 2 1
8 2 2
9 2 4
10 3 0
11 3 1
12 4 5
13 4 0
14 5 5
15 6 5
17 7 0
18 7 1
19 7 2
26 12 0
27 12 1
28 12 2
29 1 0
30 1 1
31 1 4
62 45 5
63 46 0
64 46 1
65 46 4
107 49 6
108 49 2
109 49 4
110 50 1
111 50 3
116 0 1
117 0 4
118 51 0
119 51 1
120 51 2
答案 0 :(得分:1)
您可以使用条件聚合:
select batchId
from your_table
group by batchId
having count(distinct case when subjectID in (0,1,2) then subjectID end) = 3
and count(case when subjectID not in (0,1,2) then 1 end) = 0
说明:
Group by batchId
- 在batchId上汇总count(distinct case when subjectID in (0,1,2) then subjectID end)
- 仅当此batchId中存在所有三个count(case when subjectID not in (0,1,2) then 1 end)
- 如果除了0,1,2之外没有其他subjectID,则生成0,假设subjectId列中不允许使用null。答案 1 :(得分:0)
您可以使用Row_Number()
app.get('/read', isLoggedIn, function(req, res) {
dbrw.read(req.user.id, function(result) {
console.log(result);
res.end(); // or `res.send(result)`
});
});