Decreasing the value of two fields of a table and sorting according to the result

0

I have the field "killed" and "died" in the table, I need to make the order of the results come according to the decrease of these two values,

My query looks like this:

$woes = DB::table('woeplayerrank as woe')
        ->join('char', 'woe.char_id', '=', 'char.char_id')
        ->select('woe.kills_permanente', 'woe.deaths_permanente', 'woe.killen_spree', 'woe.name', 'char.unban_time', 'char.class')
        ->orderBy('woe.kills_permanente', 'ansi')
        ->take('100');

I'd like it to be something around this:

$woes = DB::table('woeplayerrank as woe')
        ->join('char', 'woe.char_id', '=', 'char.char_id')
        ->select('woe.kills_permanente', 'woe.deaths_permanente', 'woe.killen_spree', 'woe.name', 'char.unban_time', 'char.class')
        ->orderByRaw('woe.kills_permanente - woe.deaths_permanente ansi)
        ->take('100');

Does anyone know how to do this?

    
asked by anonymous 23.10.2017 / 21:20

2 answers

0

Use parentheses to perform subtraction:

$woes = DB::table('woeplayerrank as woe')
        ->join('char', 'woe.char_id', '=', 'char.char_id')
        ->select('woe.kills_permanente', 'woe.deaths_permanente', 'woe.killen_spree', 'woe.name', 'char.unban_time', 'char.class')
        ->orderByRaw('(kills_permanente - deaths_permanente)')
        ->take('100');
    
24.10.2017 / 09:13
1

What you want in SQL would look like this:

SELECT woe.kills_permanente,
  woe.deaths_permanente,
  woe.killen_spree,
  woe.name,
  char.unban_time,
  char.class,
  woe.kills_permanente - woe.deaths_permanente AS orders
FROM woeplayerrank AS woe INNER JOIN char AS char ON woe.char_id = char.char_id
ORDER BY orders ANSI LIMIT 100
    
23.10.2017 / 21:36