编写查询以显示在 6 月份加入的唯一学生 ID。对结果进行升序排序

Write a query to display unique student ID who joined in the month of June. Sort the result in ascending order

这是我试过的-

select distinct studid 
from registration 
where to_char(doj,'MM')='june'
order by studid;

但是没有得到想要的结果

TO_CHAR() 与 MM 参数会给你 06,而不是 JUNE。您可以尝试以下查询 -

select distinct studid 
from registration 
where to_char(doj,'MM')='06'
order by studid;

而不是使用 TO_CHAR,您应该使用 oracle 特定的函数 EXTRACT -

select distinct studid 
from registration 
where extract(month from doj) = 6
order by studid;

我假设您是 运行 Oracle,正如使用 to_char() 所暗示的那样。

如果您想要在给定年份的 6 月加入的学生,比如 2019 年,那么我建议检查 doj 是否为半开间隔(这比在列):

select distinct studid
from registration
where doj >= date '2019-06-01' and doj < date '2019-07-01'
order by studid

另一方面,如果您想要在 6 月的任何一个月加入的学生:

select distinct studid
from registration
where extract(month from doj) = 6
order by studid