如何将最有帮助的评论推到顶部?

How to bump most helpful reviews to the top?

想象一下,我有一堆产品,每个产品都有一个评论部分,如果有人发表了好评,他们的评论就会得到一个赞,以告知其他用户认为它有帮助。

我有一个名为 CustomerReview 的 table 和另一个名为 likeReview 的 table,在这个 table (likeReview) 中我有一列:

isLike(值为 true、false 或 null)

我需要让这个 table 将最有帮助的评论推到列表的顶部。

我应该怎么做?

我的 SQL 试图让 isLike 这样做时出现错误,因为有些评论 (CustomerReview) 与 likeReview table。

SELECT {c.pk} 
FROM{ CustomerReview as c LEFT JOIN LikeReview as l ON {c.pk} = {l.pk} } 
WHERE {c.product}=?product 
  AND {c.LANGUAGE}=?language 
ORDER BY   {l.ISLIKE } ;

我的items.xml

        <relation code="Review2User" localized="false"
              generate="true" autocreate="true">
        <sourceElement qualifier="likeReview" type="LikeReview"
                       cardinality="many" >
        </sourceElement>
        <targetElement qualifier="customerReview" type="CustomerReview"
                       cardinality="one">
        </targetElement>
    </relation>

关系

        <typegroup name="LikeReview">
        <itemtype code="LikeReview" autocreate="true" generate="true">
            <deployment table="likeReview" typecode="15088"/>

            <attributes>
                <attribute type="Customer" qualifier="customer" >
                    <modifiers optional="false" unique="true"/>
                    <persistence type="property"/>
                </attribute>
                <attribute type="CustomerReview" qualifier="review" >
                    <modifiers optional="false" unique="true"/>
                    <persistence type="property"/>
                </attribute>
                <attribute qualifier="isLike" type="java.lang.Boolean">
                    <defaultvalue>Boolean.FALSE</defaultvalue>
                    <persistence type="property"/>
                </attribute>
            </attributes>
        </itemtype>
    </typegroup>

我认为你需要聚合。像这样:

SELECT {c.pk}
FROM { CustomerReview c LEFT JOIN
     LikeReview l
     ON {c.pk +"} = {l.pk} }
WHERE {c.product} = ?product AND
      {c.LANGUAGE +} = ?language
GROUP BY {c.pk}
ORDER BY SUM(CASE WHEN {l.ISLIKE } = "true" THEN 1 ELSE 0 END) DESC

尝试使用 ON {c.pk} = {l.customerReview}

加入
SELECT {c.pk}
FROM { CustomerReview c LEFT JOIN
     LikeReview l
     ON {c.pk} = {l.customerReview} }
WHERE {c.product} = ?product AND
      {c.LANGUAGE} = ?language
GROUP BY {c.pk}
ORDER BY SUM(CASE WHEN {l.ISLIKE} = "true" THEN 1 ELSE 0 END) DESC