今天在oracle数据库运行一句sql查询语句时报错 ORA-01791:not a Selected expression 不是 SELECTed 表达式 ,sql语句简化以后如下
select distinct id,name,sex from users where name is not null order by age
上网查询了一下好像 distinct 与order在一起出现的问题,解决方法考虑了三种
- 不使用distinct去重 ,删除distinct关键字
- ,在查询字段上添加要排序的字段 ,order by后面跟的是查询结果集中的字段。
- 如果又想使用distinct 去重 又想根据不在查询结果集的字段排序也可以先多查出排序字段然后在嵌套查询,不要该字段。
改正后:
--方法1.
select id,name,sex from users where name is not null order by age
--方法2.
select distinct id,name,sex from users where name is not null order by id
--方法3.
select T.id,T,name,T.sex from(select distinct id,name,sex,age from users where name is not null order by age)T
本文参考了其他博客,想了解详情请跳转此链接至 参考博客