Support the ongoing development of Laravel.io →
Database
Last updated 1 year ago.

quyle92 liked this thread

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');   

quyle92 liked this reply

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();

quyle92 liked this reply

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 8 years ago.

quyle92 liked this reply

1

Sign in to participate in this thread!

Eventy

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.

© 2024 Laravel.io - All rights reserved.