请求sql COUNT不返回零

时间:2018-08-29 07:13:24

标签: sql postgresql count subquery

我在网站上查找新闻的请求中存在问题,因此当我对quote_plus_normal_publi的出现进行计数时,我遇到了问题,问题是此COUNT语句恢复了主查询所有结果的加法,所以我不这样做。不知道如何解决这个问题。

SELECT date, mess, publi, profil, color, name, search,

(SELECT COUNT(quote_plus_normal_publi.id_post_normal)
FROM publi
INNER JOIN quote_plus_normal_publi
ON publi.abo_id = quote_plus_normal_publi.abo_id
WHERE publi.id_personnes = 1) AS plus

FROM publi
WHERE id_personnes = 1
ORDER BY date DESC

OUTOUT:

>

 name:     John, SIr
> 
> search:    $jhon, $sir
> 
> color:    5, 15
> 
> profil:    null, null
> 
> publi:    null, null
> 
> mess:    "HEY", "HI"
> 
> date:    1535427143, 1535627143
> 
> plus:    4, 4

1 个答案:

答案 0 :(得分:0)

您要在子查询中重复publi表-它在外部查询中。

我怀疑这是您的意图。您想使用 correlation 子句将子查询连接到外部查询:

SELECT p.*,  -- or whatever columns  you want
       (SELECT COUNT(qpnp.id_post_normal)
        FROM quote_plus_normal_publi qpnp
        WHERE p.abo_id = qpnp.abo_id
       ) as plus
FROM publi p
WHERE p.id_personnes = 1
ORDER BY p.date DESC;