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

同じテーブルに対するpostgres再帰クエリ

    where document_id=1をクエリの下部に移動したいと思います。

    ただし、再帰クエリは制約をwithに挿入しないため、そうすることには注意してください。 声明。つまり、実際にはテーブル全体をseqスキャンし、すべての可能性を再帰的に構築し、必要なものを除外します。

    実際には、SQL関数を使用したほうがよいでしょう。つまり、次のようなものです。

    create or replace function gen_links(int) returns table (doc_id int, doc_url text) as $$
    WITH  RECURSIVE generate_links(document_id,url_id) as(  
        select document_id,url_id from document_urls where document_id=$1
    UNION ALL
        select du.document_id,du.url_id from generate_links gl,document_urls du
        where gl.url_id=du.url_id 
    )
    
    SELECT * FROM generate_links GROUP BY url_id,document_id;
    $$ language sql stable;
    



    1. SQLiteデータベースのクローンを作成する

    2. ajaxphpmysqlを使用してhtmlリンククリックでデータベースを更新します

    3. アンチジョインは左アウタージョインよりも効率的ですか?

    4. すべての製品、カテゴリ、メタデータを取得するSQLクエリwoocommerce / wordpress