下面的效果很好,但是我想在里面有不同的选项
select convert(nvarchar(10),datestart,103)
from o_course
order by convert(datetime, datestart, 103) desc
但是如果我把它改成
select distinct convert(nvarchar(10),datestart,103)
from o_course
order by convert(datetime, datestart, 103) desc
我得到
留言145,第15层,第1州,第1线如果指定了select DISTINCT, ORDER BY项必须出现在select列表中。
我的目标是从datetime字段中获取所有不同的日期,并对它们降序排序。
像这样把DISTINCT从排序中分离出来。
select
convert(nvarchar(10), foo.datestart, 103)
from
(SELECT DISTINCT datestart FROM o_course) foo
order by
foo.datestart DESC
使用GROUP BY代替distinct
select convert(nvarchar(10),datestart,103)
from o_course
group by convert(nvarchar(10),datestart,103)
order by convert(datetime, datestart, 103) desc
错误消息告诉您哪里出错了。这是修复它的一种方法:
select distinct convert(nvarchar(10),datestart,103)
from o_course
order by convert(nvarchar(10), datestart, 103) desc
我想这会对你有帮助:
select somedate from (select distinct convert(nvarchar(10),datestart,103) as somedate
from o_course) t order by somedate desc