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

このクエリで完全な外部結合をエミュレートする方法は?

    基本的に、LEFTとRIGHTJOINのユニオンを実行します。

    行を3つに制限したいという点で、実際には興味深いしわがあります。それを解決するには、

    • 「左」と「右」の両方の選択を3つに制限する
    • 次に、UNIONの結果をインラインビューで使用します
    • 次に、ユニオンを再び3つ制限します

    更新 残念ながら、私が間違っていない限り、UNIONで直接これを行うことはできないため、UNIONの前にインラインビューの別のレイヤーを追加する必要があります

    UNION内の制限は、パフォーマンス上の利点を提供し、その後の制限により、正しい結果が得られます。

    SELECT title, 
       teaser, 
       nid, 
       DATE, 
       image, 
       image_tid 
    FROM   (SELECT title, 
                   teaser, 
                   nid, 
                   DATE, 
                   image, 
                   image_tid,
                   created 
            FROM   (SELECT DISTINCT n.title, 
                                    nr.teaser, 
                                    n.nid, 
                                    Date_format(From_unixtime(n.created), 
                                    '%M %e, %Y') AS 
                                    DATE, 
                                    f.filepath 
                                    AS 
                                    image, 
                                    tn_img.tid 
                                    AS 
                                    image_tid 
                                           , 
                                    n.created 
                    FROM   node n 
                           JOIN node_revisions nr 
                             ON n.nid = nr.nid 
                           LEFT JOIN content_field_related_images cfri 
                             ON ( n.nid = cfri.nid 
                                  AND cfri.delta = 0 ) 
                           LEFT JOIN content_field_att_file cfaf 
                             ON cfri.field_related_images_nid = cfaf.nid 
                           LEFT JOIN files f 
                             ON cfaf.field_att_file_fid = f.fid 
                           JOIN term_node tn2 
                             ON n.nid = tn2.nid 
                           LEFT OUTER JOIN term_node tn_img 
                             ON cfri.field_related_images_nid = tn_img.nid 
                    WHERE  n.status = 1 
                           AND n.TYPE = 'article' 
                           AND nr.body LIKE '%kimberly-clark%' 
                           AND tn2.tid = 143 
                    ORDER  BY n.created DESC 
                    LIMIT  3) tleft 
            UNION 
            SELECT title, 
                   teaser, 
                   nid, 
                   DATE, 
                   image, 
                   image_tid,
                   created  
            FROM   (SELECT DISTINCT n.title, 
                                    nr.teaser, 
                                    n.nid, 
                                    Date_format(From_unixtime(n.created), 
                                    '%M %e, %Y') AS 
                                    DATE, 
                                    f.filepath 
                                    AS 
                                    image, 
                                    tn_img.tid 
                                    AS 
                                    image_tid 
                                           , 
                                    n.created 
                    FROM   node n 
                           JOIN node_revisions nr 
                             ON n.nid = nr.nid 
                           LEFT JOIN content_field_related_images cfri 
                             ON ( n.nid = cfri.nid 
                                  AND cfri.delta = 0 ) 
                           LEFT JOIN content_field_att_file cfaf 
                             ON cfri.field_related_images_nid = cfaf.nid 
                           LEFT JOIN files f 
                             ON cfaf.field_att_file_fid = f.fid 
                           JOIN term_node tn2 
                             ON n.nid = tn2.nid 
                           RIGHT OUTER JOIN term_node tn_img 
                             ON cfri.field_related_images_nid = tn_img.nid 
                    WHERE  n.status = 1 
                           AND n.TYPE = 'article' 
                           AND nr.body LIKE '%kimberly-clark%' 
                           AND tn2.tid = 143 
                    ORDER  BY n.created DESC 
                    LIMIT  3) tright) t 
    ORDER  BY created DESC 
    LIMIT  3 
    

    更新 spencer7593とypercubeの提案を使用して、2つのUNION ALLステートメントを使用し、インラインビューを使用しない代替アプローチを次に示します。

    SELECT DISTINCT n.created, 
                    n.title, 
                    nr.teaser, 
                    n.nid, 
                    Date_format(From_unixtime(n.created), '%M %e, %Y') AS DATE, 
                    f.filepath                                         AS image, 
                    tn_img.tid                                         AS image_tid 
    FROM   node n 
           JOIN node_revisions nr 
             ON n.nid = nr.nid 
           LEFT JOIN content_field_related_images cfri 
             ON ( n.nid = cfri.nid 
                  AND cfri.delta = 0 ) 
           LEFT JOIN content_field_att_file cfaf 
             ON cfri.field_related_images_nid = cfaf.nid 
           LEFT JOIN files f 
             ON cfaf.field_att_file_fid = f.fid 
           JOIN term_node tn2 
             ON n.nid = tn2.nid 
           LEFT OUTER JOIN term_node tn_img 
             ON cfri.field_related_images_nid = tn_img.nid 
    WHERE  n.status = 1 
           AND n.TYPE = 'article' 
           AND nr.body LIKE '%kimberly-clark%' 
           AND tn2.tid = 143 
    
    UNION ALL 
    SELECT DISTINCT n.created, 
                    n.title, 
                    nr.teaser, 
                    n.nid, 
                    Date_format(From_unixtime(n.created), '%M %e, %Y') AS DATE, 
                    f.filepath                                         AS image, 
                    tn_img.tid                                         AS image_tid 
    FROM   node n 
           JOIN node_revisions nr 
             ON n.nid = nr.nid 
           LEFT JOIN content_field_related_images cfri 
             ON ( n.nid = cfri.nid 
                  AND cfri.delta = 0 ) 
           LEFT JOIN content_field_att_file cfaf 
             ON cfri.field_related_images_nid = cfaf.nid 
           LEFT JOIN files f 
             ON cfaf.field_att_file_fid = f.fid 
           JOIN term_node tn2 
             ON n.nid = tn2.nid 
           RIGHT JOIN term_node tn_img 
             ON cfri.field_related_images_nid = tn_img.nid 
    WHERE  n.status = 1 
           AND n.TYPE = 'article' 
           AND nr.body LIKE '%kimberly-clark%' 
           AND tn2.tid = 143 
           AND cfri.field_related_images_nid IS NULL 
    ORDER  BY 1 DESC 
    LIMIT 
    3 
    



    1. PDOを適切に使用してMySQLにPOSTするフォームを作成しますか?

    2. SQLServerテーブルのスキーマとデータを比較するさまざまな方法

    3. Oracle SELECTTOP10レコード

    4. データベースへの画像の挿入