想象一下,我有很多产品,每个产品都有一个“评论”部分,如果有人留下了很好的评论,他们的评论就会像是告知其他用户觉得有帮助。
我有一个名为CustomerReview的表,和另一个名为likeReview的表,并且在此表(likeReview)内有一个列:
isLike(值true,false或null)
我需要使该表成为最有用的评论,它位于列表的顶部。
我应该怎么做?
我的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>
答案 0 :(得分: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
答案 1 :(得分: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