2020-11-13 ORA-01791:not a Selected expression 不是 SELECTed 表达式

今天在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在一起出现的问题,解决方法考虑了三种

  1. 不使用distinct去重 ,删除distinct关键字
  2. ,在查询字段上添加要排序的字段 ,order by后面跟的是查询结果集中的字段。
  3. 如果又想使用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

本文参考了其他博客,想了解详情请跳转此链接至 参考博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值