选择一个表的计数到另一个表

时间:2015-08-17 18:20:32

标签: sql plsql oracle11g

我有一个 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

基本上我有一个包含文章的表,我需要包含另一个表中的用户喜欢。

3 个答案:

答案 0 :(得分:0)

我不确定你想要实现的目标,但似乎你想要计算2个表中的所有数据。您可以将查询编辑为类似的内容。

SELECT COUNT (ARTICLES.*) FROM RSKLMOBILEB2E.NEWS_LIKES LIKES
JOIN RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES 
ON LIKES.NEWS_ARTCL_ID = ARTICLES.NEWS_ARTCL_ID

答案 1 :(得分:0)

使用subquery在第一个查询中添加likescount,如下所示:

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
    ,(
        SELECT COUNT(*)
        FROM RSKLMOBILEB2E.NEWS_LIKES LIKES
        WHERE LIKES.NEWS_ARTCL_ID = ARTICLES.NEWS_ARTCL_ID
        ) AS LikesCount
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;

答案 2 :(得分:0)

我认为解决方案是使用分析函数。请查看https://oracle-base.com/articles/misc/analytic-functions

请检查以下查询(请记住,我不知道您的表结构)。由于左连接记录可能重复,这就是添加分组的原因。

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, 
  count(LIKES.ID) over ( partition by ARTICLES.NEWS_ARTCL_ID ) as num_likes
FROM RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES
join RSKLMOBILEB2E.MEDIA_TYPE MEDIA 
on ARTICLES.MEDIA_TYPE_IDENTIF = MEDIA.MEDIA_TYPE_IDENTIF 
join RSKLMOBILEB2E.ARTICLE_SOURCE SOURCES 
on ARTICLES.ARTCL_SRC_ID = SOURCES.ARTCL_SRC_ID   
LEFT JOIN RSKLMOBILEB2E.NEWS_LIKES LIKES
 ON LIKES.NEWS_ARTCL_ID = ARTICLES.NEWS_ARTCL_ID
 WHERE 
  ARTICLES.ARTCL_SRC_ID = 1 
 group by 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
ORDER BY ARTICLES.NEWS_ARTCL_PUB_DT

我还将条件的逗号分隔列表从条件更改为连接。我认为这更具可读性,因为表连接条件与where子句中的结果过滤分开。