I have the following MySQL query which works fine. It returns Random results from my table.
SET @prev=0,@rownum=0;
SELECT utilizador_id, nome
FROM (
SELECT *,
IF( @prev <> utilizador_id,
@rownum := 1,
@rownum := @rownum+1
) AS rank,
@prev := utilizador_id,
@rownum
FROM (
SELECT * FROM anuncios
ORDER BY utilizador_id, rand()
) AS random_ads
) AS ads_ranked
WHERE rank <= 2;
Here is my table:
+-------------+------+
|utilizador_id|nome |
+-------------+------|
| 1 |test1 |
| 1 |test2 |
| 1 |test3 |
| 1 |test4 |
| 1 |test5 |
| 2 |test1 |
| 2 |test2 |
| 2 |test3 |
| 3 |test1 |
| 3 |test2 |
| 3 |test3 |
+-------------+------|
Expected Random results:
+-------------+------+
|utilizador_id|nome |
+-------------+------|
| 1 |test2 |
| 1 |test5 |
| 2 |test1 |
| 2 |test2 |
| 3 |test1 |
| 3 |test3 |
+-------------+------|
The sql statement as mention works fine in MySQL but I want to implement it in my Laravel environment.
My Question: How do I implement this sql statement in Laravel?