sql >> データベース >  >> RDS >> Oracle

階層クエリ

    Oracleセットアップ

    CREATE TABLE table_name ( GRAND_MODEL, WAIT_4_MODEL_NAME ) AS
    SELECT 'test',  'test1' FROM DUAL UNION ALL
    SELECT 'test',  'test2' FROM DUAL UNION ALL
    SELECT 'test',  'test3' FROM DUAL UNION ALL
    SELECT 'test2', 'test3' FROM DUAL UNION ALL
    SELECT 'test3', 'test4' FROM DUAL UNION ALL
    SELECT 'test4', 'test5' FROM DUAL;
    

    クエリ

    SELECT REGEXP_SUBSTR( tests, '[^|]+', 1, 1 ) AS first_model,
           REGEXP_SUBSTR( tests, '[^|]+', 1, 2 ) AS second_model,
           REGEXP_SUBSTR( tests, '[^|]+', 1, 3 ) AS third_model,
           REGEXP_SUBSTR( tests, '[^|]+', 1, 4 ) AS fourth_model,
           REGEXP_SUBSTR( tests, '[^|]+', 1, 5 ) AS fifth_model,
           REGEXP_SUBSTR( tests, '[^|]+', 1, 6 ) AS sixth_model,
           REGEXP_SUBSTR( tests, '[^|]+', 1, 7 ) AS seventh_model,
           REGEXP_SUBSTR( tests, '[^|]+', 1, 8 ) AS eighth_model,
           REGEXP_SUBSTR( tests, '[^|]+', 1, 9 ) AS ninth_model
    FROM   (
      SELECT SYS_CONNECT_BY_PATH( wait_4_model_name, '|' ) || '|' ||  grand_model AS tests
      FROM  table_name
      CONNECT BY PRIOR grand_model = wait_4_model_name
    );
    

    出力

    FIRST_MODEL SECOND_MODEL THIRD_MODEL FOURTH_MODEL FIFTH_MODEL SIXTH_MODEL SEVENTH_MODEL EIGHTH_MODEL NINTH_MODEL
    ----------- ------------ ----------- ------------ ----------- ----------- ------------- ------------ -----------
    test1       test                                                                                                 
    test2       test                                                                                                 
    test3       test                                                                                                 
    test3       test2                                                                                                
    test3       test2        test                                                                                    
    test4       test3                                                                                                
    test4       test3        test                                                                                    
    test4       test3        test2                                                                                   
    test4       test3        test2       test                                                                        
    test5       test4                                                                                                
    test5       test4        test3                                                                                   
    test5       test4        test3       test                                                                        
    test5       test4        test3       test2                                                                       
    test5       test4        test3       test2        test                                                           
    


    1. Bツリーとハッシュテーブル

    2. Mysql:いいねで注文しますか?

    3. インデックスの列サイズが大きすぎます。最大列サイズは767バイトです。 -INTデータ型の場合?

    4. PHPのin_arrayとMySQLSELECT