`
zengguo1988
  • 浏览: 280122 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

not a SELECTed expression

阅读更多

异常信息:There is an incorrect ORDER BY item. The query is a SELECT DISTINCT query with an ORDER BY clause. In this context, all ORDER BY items must be constants, SELECT list expressions, or expressions whose operands are constants or SELECT list expressions. 
   There is an incorrect ORDER BY item. The query is a SELECT DISTINCT query with an ORDER BY clause. In this context, all ORDER BY items must be constants, SELECT list expressions, or expressions whose operands are constants or SELECT list expressions.

 

 

 大致就是说select distinct 和order by一起使用的时候,order by中必须是常量或者select列表中出现的表达式。

 例如: select distinct  t.name from user t  order by t.modified_date ASC

这样就会出现错误。

其实上面的说法不完全正确。对于单表来讲,如果name这个字段有unique index 并且字段有not null约束。那么这条语句是不会出错的。

如果是多个表join,这种情况我经过大量测试,发现仍然会出错,所以这种特殊性目前看来只存在于单表中。使用中大家注意。

至于为什么会出现这种特殊性,其他的要出错,而唯独满足以上红色部分条件不错,原因大概是这样的:

 假设name不唯一,返回时存在如下三条记录

  name   modified_date

   A          2008-11-12

   A          2008-11- 14

   B          2008-11-13

    执行distinct 后得到 A 、B (注意,这里没有管顺序),然后根据modified_date字段来确定A、B记录的先后顺序,这个时候问题就来了,order by根据哪个modified_date来排序来确定A的位置呢?如果根据第一个,那么结果应该是AB,根据第二个结果应该是BA,确定不了了。。。。所以当然会报错。

如果name唯一,查询出来的记录即使不distinct也是唯一的,这一点数据库自己是明白的,那么order by时,当然可以排出序来。这样就不会出错。

所以不管是单表还是多表,只要oracle自己区分不出根据哪个来排序就会出现这个异常。

 

个人博客正式上线,欢迎访问

分享到:
评论

相关推荐

    2009 达内Unix学习笔记

    [] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件。 $ 取环境变量的值。 | 管道。把前一命令的输出作为后一命令的输入,把几个命令连接起来。 |经常跟tee连用,tee 把内容保存到文档并显示出来。 三、通用后...

    UE(官方下载)

    The selected text compare allows you to select portions of text between 2 files and execute a compare on ONLY the se Using the SSH/telnet console A tutorial for UltraEdit/UEStudio's SSH/telent ...

    DevExpress VCL 13.2.5 D7-DXE6 FullSource

    •Q581933 - At design time, modifying any property of a control selected on the parent form selects a control on an inherited form if the parent form contains TdxWizardControl and/or ...

    apache-maven-3.0.2-bin

    * [MNG-4960] - [regression] Make-like reactor mode does not build selected project when resuming from one of its prerequisites * [MNG-4966] - Preserve double slashes in the scm connection url - ...

    UG6.0快捷键大全

    POPUP_MESSAGE Groups the selected features into a collection called a feature set. ACTIONS STANDARD END_OF_AFTER MENU UG_ARRANGE BUTTON/HIDE UG_MODELING_EDIT_GROUP_FEATURE LABEL Edit... ...

    Delphi7.1 Update

    visit the Borland Registered User web site to obtain a localized readme file that may contain important late- breaking information not included in this readme file.IMPORTANT: Delphi must be closed ...

    Rapid GUI Programming with Python and Qt

    An Expression Evaluator in 30 Lines 131 A Currency Converter in 70 Lines 136 Signals and Slots 142 Summary 151 Exercise 152 Chapter 5. Dialogs 154 Dumb Dialogs 156 Standard Dialogs 162 Smart ...

    SQLPrompt_7.3.0.651(包含注册机)

    Fix for script not being formatted if there are errors outside of the selected text (UserVoice) Whitespace at the end of the file is now maintained if "Preserve existing new lines between statements" ...

    一个win32下的ARM开源编译器

    pre-UAL syntax does not specify the "s" (flag update) in the opcodes for arithmetic operations involving the low set of registers (r0-r7). For example: pre-UAL Syntax | UAL Syntax ;Note -----------...

    Turbo C++ 3.0[DISK]

    If you are not a member of CompuServe, see the enclosed special offer, and write for full details on how to receive a free IntroPak containing a $15 credit toward your first month's on-line ...

    Turbo C++ 3.00[DISK]

    If you are not a member of CompuServe, see the enclosed special offer, and write for full details on how to receive a free IntroPak containing a $15 credit toward your first month's on-line ...

    Visual Assist X 2107官方原版 带破解补丁

    Fix for failure of Goto to work with variable defined using decltype expression. (case=93387) 12485 Various fixes for Smart Select selections. (case=94946, case=95812) Improved auto type deduction in ...

    jQuery完全实例.rar

    This function also accepts XML Documents and Window objects as valid arguments (even though they are not DOM Elements). 返回值 jQuery 参数 elements (Element, Array) : 用于封装成jQuery对象的DOM元素 ...

    Borland Delphi 2005 Architect Update 3

    project loading is very SLOW4.00 of 5 Closed10314 Error Insight fails to flag an error4.00 of 5 Closed10248 Alignment Palette in VCL form designer malfunctions5.00 of 5 Closed10190 List view selected ...

    sqlmap (懂的入)

    header string randomly selected from a text file; * It is possible to provide a HTTP Cookie header string, useful when the web application requires authentication based upon cookies and you have ...

    正则表达式

    对正则表达式功能字符的还原,如"*"匹配它前面元字符0次或多次,/a*/将匹配a,aa,aaa,加了"\"后,/a\*/将只匹配"a*"。 ^ 匹配一个输入或一行的开头,/^a/匹配"an A",而不匹配"An a" $ 匹配一个输入或一行的结尾,/a...

    arcgis工具

    先选责需要另存图层的要素(直接选择或者属性表),然后在图层上右键Selection/create Layer From Selected Features。 (以前都是直接export竟然没有发现) 27.利用Hyperlinks丰富数据库,为其添加超链接。 单击...

    jQuery-1.3.2 学习笔记(转).txt

    - `jQuery(expression, [context])`:根据 CSS 选择器在给定的上下文中选取文档中的元素。 - `jQuery(html, [ownerDocument])`:根据给定的 HTML 字符串创建新的 DOM 元素。 - `jQuery(elements)`:将一组 DOM ...

Global site tag (gtag.js) - Google Analytics