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

Asked

Viewed 54 times

0

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

My query is 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 wish it was something around that:

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

Can anyone tell me how to do that?

2 answers

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
  • Sorry my inexperience, but after a few attempts I could not make your solution run ! would have some alternative means?

0


Use parentheses to subtract:

$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');
  • 1

    Just for the record, the fields cannot be marked as unsigned in the table.

Browser other questions tagged

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