Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 3 years ago.
1

Have you thought about using a stored procedure?

0
Solution

This seems work for me...What were you trying to do?

DB::statement(DB::raw('SET @total = "bar"'));
$a = DB::select(DB::raw('select * from applications where foo = @total'));
    
DB::statement(DB::raw('SET @total = "id"'));
$b = DB::select('select @total from applications');   
1

There's inline initializing available in MySQL:

DB::select(DB::raw('@total := @total + x as total_x'), ...)
   ->from(DB::raw('your_table, (select @total := 0) as var'))
   ... 
   ->get();
1

could you help me to put this query in laravel

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()
  ) random_prodcts
) products_ranked
WHERE rank <= 2

thanks in advance

Last updated 10 years ago.
1

Sign in to participate in this thread!

Native PHP

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.