2

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

我有一个名为 CustomerReview 的表,还有一个名为 likeReview 的表,在这个表 (likeReview) 内我有一列:

isLike(值为真、假或空)

我需要让这张表将最有用的评论排在列表顶部。

我该怎么做呢?

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

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>
4

2 回答 2

1

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

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
于 2019-05-16T01:13:52.870 回答
0

尝试加入使用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
于 2019-05-16T17:09:24.853 回答