I am somewhat stuck right now and could use some quick MySQL help.
Say I have a table containing reviews for products, columns product_id
, customer
, date
.
Now for each distinct product, I want to get the 3 first reviews.
ORDER BY date is straightforward, but I'm stuck on how to get exactly 3 entries for each distinct product_id. Since it's all in the same table, INNER JOIN doesn't really make any sense, neither does GROUP BY.
So, any ideas?
Edit: I've been successfully using this query on SQLFiddle:
SELECT * FROM (
SELECT customer, DATE, p_asin, @curTop :=
CASE WHEN (
@prevP_asin <> p_asin
)
THEN 1
ELSE @curTop +1
END AS Top, @prevP_asin := p_asin
FROM reviews2 r, (
SELECT @curTop :=0
) A
ORDER BY r.p_asin, r.date ) B
WHERE top <=3
But for some reason, when I try to apply it to my tables in PhpMyAdmin, the numbering with Top doesn't start by 1 for new products, instead counts all together. How can Fiddle and the actual table behave so differently?