我的SQL查询显示用户评论的最有用的评论

时间:2014-10-28 06:15:16

标签: jquery mysql sql

我想运营一个电子商务网站,然后去销售产品。我希望获得评分&从每个用户审查,以提高质量。所以在一个单独的页面上,我想获得评级&用户评论。因此,其他用户可以将评论标记为有用或无用。因此,当用户选择发布的评论是否有用时,我将在新表中创建一个DB条目。 所以我的要求是,在新页面上,我想根据用户选择中最有用的评论列出评论。

所以我想首先根据大多数有用的评论显示结果(按降序排列)

我的表格结构如下。 rating_review表: enter image description here rating_review_helpful表

enter image description here

我已在下面粘贴了我的SQL查询。

SELECT *,
       rating_reviews_helpful.rating_id,
       COUNT(*) AS COUNT
FROM `rating_reviews_helpful`
JOIN rating_reviews ON rating_reviews.shop_id = rating_reviews_helpful.deal_id
JOIN users ON users.user_id=rating_reviews.user_id
WHERE rating_reviews_helpful.review_helpful =1
  AND rating_reviews.rating_status = 1
  AND rating_reviews_helpful.deal_id = 12
  AND rating_reviews_helpful.type = 2
GROUP BY rating_reviews_helpful.rating_id
ORDER BY COUNT DESC

以上查询的结果如下。 enter image description here

我已经坚持了很久。任何人都可以帮我摆脱这个吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

You can try the following query to get the result

  SELECT (add all the fields you require) ,B.rating_id
FROM    `rating_reviews_helpful` rrh1
JOIN rating_reviews rr1 ON rr1.shop_id = rrh1.deal_id
JOIN users u1 ON u1.user_id=rr1.user_id JOIN (SELECT rrh.rating_id rating_id,
       COUNT(*) AS COUNT
FROM `rating_reviews_helpful` rrh
JOIN rating_reviews rr ON rr.shop_id = rrh.deal_id
JOIN users u ON u.user_id=rr.user_id
WHERE rrh.review_helpful =1
  AND rr.rating_status = 1
  AND rrh.deal_id = 12
  AND rrh.type = 2
GROUP BY rrh.rating_id) B ON rrh1.rating_id = B.rating_id
WHERE rrh1.review_helpful =1
  AND rrh1 = 1
  AND rrh1.deal_id = 12
  AND rrh1.type = 2
ORDER BY B.COUNT DESC