一个查询,用于计算来自另一个表的记录数

时间:2019-02-22 05:36:26

标签: sql postgresql

我有桌子merchants

merchant_id |  merchant_name  | ... |
-------------------------------------
      1     | merchant name 1 | ... |
      2     | merchant name 2 | ... |
      3     | merchant name 3 | ... |

产品表products

 product_id |  product_name  | ... |
------------------------------------
      1     | product name 1 | ... |
      2     | product name 2 | ... |
      3     | product name 3 | ... |

还有表likes_products

 like_id |  product_Id  | user_id |
-----------------------------------
    1    |      101     |   101   |
    2    |      102     |   102   |
    3    |      103     |   101   |

likes_merchants

 like_id |  merchants_Id  | user_id |
-----------------------------------
    1    |       107      |   101   |
    2    |       108      |   102   |
    3    |       109      |   101   |

bookmarks_products

 bookmark_id |  product_Id  | user_id |
---------------------------------------
       1     |      101     |   101   |
       2     |      102     |   102   |
       3     |      103     |   101   |

我要求显示所有产品,并计算其点赞次数。然后,他看起来像现在是已授权产品的用户中的用户,并且如果该用户具有产品选项卡,并显示truefalse

SELECT P.* , 
  COUNT(L.USER_ID) AS LIKES,
  (B.PRODUCT_ID IS NOT NULL) AS BOOKMARKS,
  (L.PRODUCT_ID IS NOT NULL) AS IS_LIKED
    FROM PRODUCTS AS P
      LEFT JOIN BOOKMARKS_PRODUCTS AS B ON (B.PRODUCT_ID = P.PRODUCT_ID)
      LEFT JOIN LIKES_PRODUCTS AS L ON (L.PRODUCT_ID = P.PRODUCT_ID)
        GROUP BY P.PRODUCT_ID, B.PRODUCT_ID, L.PRODUCT_ID
          ORDER BY P.PRODUCT_ID

我的问题是: 1)我无法检查授权用户喜欢或喜欢的书签。如果有任何来电者喜欢某产品或将其添加为书签,他将释放true

2)我不明白为什么他会同时考虑喜欢和收藏的数量。也就是说:如果用户在某个商品上添加了一个喜欢的商品并为其添加了书签,则该商品被添加了一个喜欢的商品,然后另一个用户为该商品添加了书签,那么喜欢的商品就增加了,而如果他喜欢此商品,则该商品会加倍。

请帮助我了解我的问题

我希望收到的东西:

 product_id |  product_name  | ... | LIKES | BOOKMARKS | IS_LIKED |
-------------------------------------------------------------------
      1     | product name 1 | ... |   1   |   false   |   true   |
      2     | product name 2 | ... |   0   |   true    |   true   |
      3     | product name 3 | ... |   5   |   false   |   false  |

但是问题是,如果table, there are bookmarks from users in the表中有来自用户的赞,它将对其进行汇总并输出正确的一半赞。

在没有GROUP BY的情况下会发生以下情况:

SELECT P.* , 
  (B.PRODUCT_ID IS NOT NULL) AS BOOKMARKS,
  (L.PRODUCT_ID IS NOT NULL) AS IS_LIKED
    FROM PRODUCTS AS P
      LEFT JOIN BOOKMARKS_PRODUCTS AS B ON (B.PRODUCT_ID = P.PRODUCT_ID)
      LEFT JOIN LIKES_PRODUCTS AS L ON (L.PRODUCT_ID = P.PRODUCT_ID)
          ORDER BY P.PRODUCT_ID
 product_id |  product_name  | ... | LIKES | BOOKMARKS | IS_LIKED |
-------------------------------------------------------------------
      1     | product name 1 | ... |   1   |   false   |   true   |
      1     | product name 1 | ... |   1   |   false   |   true   |
      3     | product name 3 | ... |   5   |   false   |   false  |

产品1在两个表likes_productsbookmarks_products中被两次输入,

1 个答案:

答案 0 :(得分:1)

您可以在下面尝试-

select A.product_id,likes,
case when B.PRODUCT_ID IS NOT NULL then 'true' else 'false' end AS BOOKMARKS,
case when L.PRODUCT_ID IS NOT NULL then 'true' else 'false' end AS IS_LIKED
from
(  
SELECT P.PRODUCT_ID , 
  COUNT(L.USER_ID) AS LIKES,
  FROM PRODUCTS AS P
  LEFT JOIN LIKES_PRODUCTS AS L ON L.PRODUCT_ID = P.PRODUCT_ID
  GROUP BY P.PRODUCT_ID
)A 
LEFT JOIN BOOKMARKS_PRODUCTS AS B ON B.PRODUCT_ID = A.PRODUCT_ID
LEFT JOIN LIKES_PRODUCTS AS L ON L.PRODUCT_ID = A.PRODUCT_ID
order by A.product_id