Hi everybody,
I have spent several hours with this SQL problem, which I thought would be easy - I still think it should be, but obviously not easy enough for me (not an SQL expert). I would be glad if you could help me with this.
I have stripped this down for this example. Imagine two tables:
PRODUCT
-ID
-NAME
REVIEW
-ID
-PRODUCT_ID
-USER_ID
-CONTENT
Where each user can review a product only once.
What I need now is an SQL query that returns all products together with the reviews of a given user. If a user has not reviewed a product, there should be NULL values returned for the review columns.
I thought something like the following would do, but obviously it does not, since only reviewed products are returned.
SELECT p.*, r.*
from PRODUCT p join REVIEW r
on r.PRODUCT_ID = p.ID
where r.USER_ID=:userId
I would be veeeeery happy if someone could help me out with this. I am pretty sure there has to be some kind of subselect involved, but I do not seem to be able to figure it out myself :(
BTW: I need to translate this to HQL, so the solution should not make use of features like UNION, which are not supported in HQL.
Thanks a lot & best regards, Peter