jemz
jemz

Reputation: 5153

Sum multiple fields in 1 table

Hi I would like to sum my 4 columns in my table. but I get error he SUM function requires 1 argument(s) itemcost table

+------+------+------+------+------+
| id   | col1 | col2 | col3 | col4 |
+======+======+======+======+======+
| 0002 | 5    | 5    | 5    | 5    |
+------+------+------+------+------+
|      |      |      |      |      |
+------+------+------+------+------+
|      |      |      |      |      |
+------+------+------+------+------+


$cost= DB::table('itemcost')
            ->select(
                DB::raw('SUM(col1,col2,col3,col4) as unitprice')
            );

Thank you in advance.

Upvotes: 0

Views: 55

Answers (2)

You could add columns with + sign, Try like bellow:

      $cost= DB::table('itemcost')  
        ->select(
            DB::raw('SUM(col1+col2+col3+col4) as unitprice')
        );

Upvotes: 0

Thamilhan
Thamilhan

Reputation: 13323

To sum just column of every single row, use:

(col1+col2+col3+col4) as unitprice

Or, to sum columns with rows, use:

(SUM(col1)+SUM(col2)+SUM(col3)+SUM(col4)) as unitprice

By the way, here is an article with examples

Upvotes: 2

Related Questions