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

3

I wanted to convert this query as I could use it in Lavarel DB \ Facades

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 position rank of the player by the order of Kills of the whole server.

Update

When using whereRaw , as suggested, the following error occurs

Because before it does the assembly of SELECT *

    
asked by anonymous 27.09.2017 / 15:44

2 answers

1

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

See:

$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);
    
27.09.2017 / 17:16
0

You can use the whereRaw() method 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();
    
27.09.2017 / 16:50