Oracle-11g-R2

選擇一張表的計數到另一張表中

  • August 18, 2015

我有一個 SQL 語句:

SELECT   ARTICLES.NEWS_ARTCL_ID, ARTICLES.NEWS_ARTCL_TTL_DES,
        ARTICLES.NEWS_ARTCL_CNTNT_T, ARTICLES.NEWS_ARTCL_PUB_DT, 
        ARTICLES.NEWS_ARTCL_AUTH_NM, ARTICLES.NEWS_ARTCL_URL,
        ARTICLES.MEDIA_URL, ARTICLES.ARTCL_SRC_ID, SOURCES.ARTCL_SRC_NM,
        MEDIA.MEDIA_TYPE_DESCRIP
FROM     RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES,
        RSKLMOBILEB2E.MEDIA_TYPE MEDIA, 
        RSKLMOBILEB2E.ARTICLE_SOURCE SOURCES
WHERE    ARTICLES.MEDIA_TYPE_IDENTIF = MEDIA.MEDIA_TYPE_IDENTIF
AND      ARTICLES.ARTCL_SRC_ID = SOURCES.ARTCL_SRC_ID  
AND      ARTICLES.ARTCL_SRC_ID = 1
ORDER BY ARTICLES.NEWS_ARTCL_PUB_DT;

現在我需要將另一個 SQL 語句組合成一個:

SELECT   COUNT(*)
FROM     RSKLMOBILEB2E.NEWS_LIKES LIKES, 
        RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES
WHERE    LIKES.NEWS_ARTCL_ID = ARTICLES.NEWS_ARTCL_ID;

由於我是 Oracle 的新手,因此不確定如何實現這一點。基本上我有一個包含文章的表,我需要包含另一個表中的使用者喜歡。

非常感謝您對此的任何幫助。

嘗試以下操作:

SELECT   ART.NEWS_ARTCL_ID, ART.NEWS_ARTCL_TTL_DES,
        ART.NEWS_ARTCL_CNTNT_T, ART.NEWS_ARTCL_PUB_DT,
        ART.NEWS_ARTCL_AUTH_NM, ART.NEWS_ARTCL_URL,
        ART.MEDIA_URL, ART.ARTCL_SRC_ID, SRC.ARTCL_SRC_NM,
        MED.MEDIA_TYPE_DESCRIP, LIK.TOTAL
FROM     RSKLMOBILEB2E.NEWS_ARTICLE ART,
        RSKLMOBILEB2E.MEDIA_TYPE MED,
        RSKLMOBILEB2E.ARTICLE_SOURCE SRC,
        (SELECT   AR1.NEWS_ARTCL_ID, COUNT(*) TOTAL
         FROM     RSKLMOBILEB2E.NEWS_ARTICLE AR1,
                  RSKLMOBILEB2E.NEWS_LIKES LK1
         WHERE    AR1.NEWS_ARTCL_ID      = LK1.NEWS_ARTCL_ID) LIK
WHERE    ART.MEDIA_TYPE_IDENTIF = MED.MEDIA_TYPE_IDENTIF
AND      ART.ARTCL_SRC_ID       = SRC.ARTCL_SRC_ID
AND      ART.ARTCL_SRC_ID       = 1
AND      ART.NEWS_ARTCL_ID      = LIK.NEWS_ARTCL_ID
ORDER BY ART.NEWS_ARTCL_PUB_DT;

引用自:https://dba.stackexchange.com/questions/111315