How to perform a query of a query stored in a string through the DB class?

Asked

Viewed 49 times

3

I wanted to convert this query how to use it in DB Facades Lavarel

SELECT FIND_IN_SET(KillCount, (SELECT GROUP_CONCAT(KillCount ORDER BY KillCount DESC) FROM users)) AS Rank FROM users WHERE id = 1

It takes the player’s rank position in the order of Kills from the entire server.

Updating

When using the whereRaw, as suggested, the following error occurs

inserir a descrição da imagem aqui

For before he makes the montage of SELECT *

  • 2

    Good afternoon, young man. I could copy the code of the error generated and post here, so we can format it in a more appropriate way?

2 answers

1

You can use the method DB::select directly. It allows you to execute a query from a string.

Behold:

$query = 'SELECT FIND_IN_SET(KillCount, (SELECT GROUP_CONCAT(KillCount ORDER BY KillCount DESC) FROM users)) AS Rank FROM users WHERE id = 1';

$resultado = DB::select($query);
  • In such cases I usually use a lot when I need to debug something through the SHOW TABLES.

0

You can use the method whereRaw() of Facade, it would look something like this:

\DB::table("users")->whereRaw("FIND_IN_SET(KillCount, (SELECT GROUP_CONCAT(KillCount ORDER BY KillCount DESC) FROM users)) AS Rank")
       ->where('id', 1)
       ->get();
  • 1

    According to the Questioner’s comment, your reply causes an error.

  • 1

    Really @Wallacemaxters, at the time I couldn’t test and now I tested and saw that I was wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.