修改oracle查询开始

时间:2012-08-24 07:04:43

标签: sql oracle

我正在使用其他开发人员编写的以下查询:

SELECT DISTINCT c.id category_id,
  c.parent_id,
  d.name AS category_name,
  level
FROM category c,
  category_language d
WHERE c.id                                 = d.category_id
AND c.is_active                            = 1
AND c.is_deleted                           = 0
AND c.deleted_date                        IS NULL
AND d.is_active                            = 1
AND d.is_deleted                           = 0
AND d.deleted_date                        IS NULL
AND ((to_date(d.expiry_date,'DD-MON-YYYY') > to_date(sysdate,'DD-MON-YYYY'))
OR d.expiry_date                          IS NULL)
AND d.language_id                          = 1
AND c.cat_type_id                          = 1
  START WITH c.parent_id                   =1308206844
  CONNECT BY c.parent_id                   = prior c.id
ORDER SIBLINGS BY d.name 
我有两个问题。

首先,我不知道如何 START WITH CONNECT BY PRIOR 关键字正在查询中。

其次,当我将 ORDER SIBLINGS BY d.name 更改为 ORDER SIBLINGS BY c.priority desc 时,会抛出以下错误消息:=

ORA-01791: not a SELECTed expression
01791. 00000 -  "not a SELECTed expression"

我还尝试将数据类型的优先级从数字更改为字符串,但会抛出相同的错误。

我想通过c.priority desc运行命令查询以产生所需的结果。

1 个答案:

答案 0 :(得分:3)

关键字START WITH, CONNECT BY, PRIOR用于显示层次结构。在我看来,这个链接是一个非常好的教程:http://www.adp-gmbh.ch/ora/sql/connect_by.html

至少我按照例子学会了如何使用它。也许这也为你提供了ORDER SIBLINGS BY真正含义的答案,然后你可以自己回答如何避免错误信息。显然Oracle希望您在所选列中包含该列。 ORDER SIBLINGS特定于层次结构,您可以谷歌搜索它。但我认为您首先需要了解START WITH, CONNECT BY, PRIOR

也许你不想只做一个ORDER SIBLINGS BY而只想ORDER BY