Picture this, I have a bunch of products, each product has a review section, if someone leaves a good review their review gets a like to inform that other users found it helpful.
I have a table named CustomerReview, and another table named likeReview, and inside this table (likeReview) I have a column:
isLike(values true, false or null)
I need to make this table bump the most helpful reviews to the top of the list.
How should I go about that?
I get an error with My SQL trying to make isLike do that, because there are reviews (CustomerReview) that are not associated with the 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 } ;
my 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>
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>
Try to join using 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