1

我是 sql 的新手。

我有这张表,上面有这些记录:

桌子mark

----------------------------------------
student_id | subject_id | comp_id | mark
----------------------------------------
 1         |  1         |   28    |  5.5
 1         |  1         |   29    |  7.5
 1         |  1         |   30    |  9.0
 1         |  2         |   36    |  9.6
 1         |  2         |   37    |  6.0
 1         |  3         |   42    |  4.0

我试过这个查询SELECT subject_id FROM mark WHERE student_id =1

结果

subject_id
----------
1
1
1
2
2
3

如何选择具有这些结果的数据?我期待这样的事情:

subject_id
----------
1
2
3

提前致谢。

4

4 回答 4

1

只需使用DISTINCT

SELECT DISTINCT subject_id FROM mark WHERE student_id =1
于 2013-10-24T18:39:58.970 回答
0

利用 distinct

SELECT distinct subject_id 
FROM mark 
WHERE student_id = 1

或对数据进行分组

SELECT subject_id 
FROM mark 
WHERE student_id = 1
group by subject_id
于 2013-10-24T18:39:54.813 回答
0

使用DISTINCT子句就可以实现

SELECT DISTINCT subject_id FROM mark WHERE student_id =1;

然后你会得到输出

subject_id
----------
1
2
3
于 2013-10-24T18:45:42.510 回答
0

您可以使用GROUP BY

SELECT subject_id FROM mark WHERE student_id =1 GROUP BY student_id;
于 2013-10-24T18:48:20.073 回答