English 中文(简体)
页: 1
原标题:Oracle - SQL with recursive WHERE clause

我有一个表格,储存本组织各工作组的等级。 它想像:

CREATE TABLE WORKGROUPS  ( 
    WORKGROUPID         NUMBER NULL,
    NAME                VARCHAR2(100) NOT NULL,
    PARENTWORKGROUPID   NUMBER NOT NULL,
    WORKGROUPLEVEL      CHAR(1) DEFAULT 1 NOT NULL,
    CONSTRAINT WORKGROUPS_PK PRIMARY KEY(WORKGROUPID)
)

例如,可能有三个层次的工作组:

ID 1 - Sales (PARENTWORKGROUPID = 0)
ID 2 - Business Sales (PARENTWORKGROUPID = 1)
ID 3 - West Coast B2B (PARENTWORKGROUPID = 2)

因此,3名家长为2名,其中2名为1名,1名为无父母的高层工作团体,因此我们使用0名。

现在我们有一个TASKS表格。 每个TASKS row都有一个工作文件栏,该栏在工作清单表中注明了工作组。

我需要写一个疑问,即把属于某个高级别工作组的所有图瓦卢皇家警察部队,例如销售的所有物品(如上所述,这些产品可以是1、2或3个工地综合数据库)。 从根本上说,它是一个退步的问题。

我可以考虑采取某种办法,利用LEFT JOIN检查各个层次,但我却不采用数据库设计以来在数量上难以编码的解决办法,以允许任何等级。 我认为,任何其他解决办法都涉及改变表象,我现在可以这样做。 任何想法? 感谢!

最佳回答

甲骨文似乎有自己的再入侵版本(IQ使用服务器和DB2,后者使用共同表格表达器进行电击),但我认为这或许会得到你们的希望:

WITH WGS (
    WORKGROUPID,
    NAME
) AS (
    SELECT WORKGROUPID,
        NAME
    FROM WORKGROUPS
    START WITH WORKGROUPID = :top-lev-dept
    CONNECT BY WORKGROUPID = PRIOR PARENTWORKGROUPID
)

SELECT DISTINCT T.*
FROM TASKS
INNER JOIN WGS W
    ON (T.WORKGROUPID = W.WORKGROUPID)

页: 1

如果无法做到这一点,请查看。 这可能使你朝着正确的方向开始......

问题回答

从Oracle 11gR2开始,ANSI与yn合的休妻得到支持,并且是《裁武条约》/《裁武条约》的替代办法:

WITH wgs ( workgroupid, name ) AS
(
    SELECT workgroupid, name FROM workgroups WHERE workgroupid = :top-lev-dept
     UNION ALL
    SELECT w.workgroupid, w.name FROM workgroups w, wgs WHERE parentworkgroupid = wgs.workgroupid
)
SELECT ...

关键是:

WITH view (column_definition) AS
(
    SELECT root rows
     UNION ALL
    SELECT sub rows FROM table JOIN view ON recursion condition
)

页: 1 页: 1

类似:

select * from workgroups
start with parentworkgroupid = 0
connect by prior workgroupid = parentworkgroupid;

完全没有经过测试,但我认为这将开始。

希望会有所帮助。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签