Multiply column values in SQL, then add all the values - Laravel 4

Federico Piragua

I have this code:

DB::table('product_list')->select(DB::raw('(value*quantity) as total'))->get();

and this is my DB:

product = id,quantity,value,color

And I want them to be added all like:

1|10|500|blue
2|20|250|red
total for each = value*quantity
total = 10000

But it gives me:

array(2) { [0]=> object(stdClass)#266 (1) { ["total"]=> int(5000) } [1]=> object(stdClass)#263 (1) { ["total"]=> int(5000) } }

And the only way to sum it all is by running a foreach loop in PHP but I need to do it directly from the DB. But there must be another way, doesn't it?

Joseph Silber

Use the SQL SUM function:

$total = DB::table('product_list')
           ->selectRaw('SUM(value * quantity) as total')
           ->pluck('total');

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

What is the SQL query in Access to replace all column values with one of it's values, which depends on values of other attributes?

분류에서Dev

Grabbing all rows from a database that have specific column values in laravel

분류에서Dev

Select distinct rows where all values in a column are the same SQL Server

분류에서Dev

Add column with accumulative count of unique values in a column

분류에서Dev

CFDump of query column does not display all values

분류에서Dev

How to fetch all column values based on email?

분류에서Dev

laravel retrieve multiple values separated by commas in a column as separate values

분류에서Dev

C # 및 MS Access-SQL for Create Table, Column and add values at the start of the app

분류에서Dev

In standard SQL, how do I select rows such that for each unique value in one column, all of the values in another column are a specified value?

분류에서Dev

Automatically Assign Values To $fillable attribute (Laravel 4)

분류에서Dev

SQL aggregate multiple values in a column then Pivot

분류에서Dev

SQL: Perform arithmetic operations on values in a column

분류에서Dev

r column values in sql where statement

분류에서Dev

How to repeat all column values for each row in another column

분류에서Dev

add values depending on a condition sql-server

분류에서Dev

Add new column based on boolean values in a different column

분류에서Dev

Multiply nested pairs of list values in Clojure

분류에서Dev

How to get all the values of one column and calculate a total

분류에서Dev

Obtain count of all possible values of a given column in psql

분류에서Dev

Add column to Data Frame based on values of other columns

분류에서Dev

Add a new calculated column from 2 values in RDD

분류에서Dev

The SQL Column name or number of supplied values does not match table definition

분류에서Dev

SQL copy an existing columns values into a custom column in a select statement

분류에서Dev

sql: Getting 3 highest values from table in column format

분류에서Dev

SQL Server Display only lines with all values not equal to 0

분류에서Dev

Remove columns from SQL output where all values are 0

분류에서Dev

I am trying to add 2 values in a SQL Select Statement

분류에서Dev

Filling in column based on column values

분류에서Dev

mysql query: searching based on all values in column rather than one value in column

Related 관련 기사

  1. 1

    What is the SQL query in Access to replace all column values with one of it's values, which depends on values of other attributes?

  2. 2

    Grabbing all rows from a database that have specific column values in laravel

  3. 3

    Select distinct rows where all values in a column are the same SQL Server

  4. 4

    Add column with accumulative count of unique values in a column

  5. 5

    CFDump of query column does not display all values

  6. 6

    How to fetch all column values based on email?

  7. 7

    laravel retrieve multiple values separated by commas in a column as separate values

  8. 8

    C # 및 MS Access-SQL for Create Table, Column and add values at the start of the app

  9. 9

    In standard SQL, how do I select rows such that for each unique value in one column, all of the values in another column are a specified value?

  10. 10

    Automatically Assign Values To $fillable attribute (Laravel 4)

  11. 11

    SQL aggregate multiple values in a column then Pivot

  12. 12

    SQL: Perform arithmetic operations on values in a column

  13. 13

    r column values in sql where statement

  14. 14

    How to repeat all column values for each row in another column

  15. 15

    add values depending on a condition sql-server

  16. 16

    Add new column based on boolean values in a different column

  17. 17

    Multiply nested pairs of list values in Clojure

  18. 18

    How to get all the values of one column and calculate a total

  19. 19

    Obtain count of all possible values of a given column in psql

  20. 20

    Add column to Data Frame based on values of other columns

  21. 21

    Add a new calculated column from 2 values in RDD

  22. 22

    The SQL Column name or number of supplied values does not match table definition

  23. 23

    SQL copy an existing columns values into a custom column in a select statement

  24. 24

    sql: Getting 3 highest values from table in column format

  25. 25

    SQL Server Display only lines with all values not equal to 0

  26. 26

    Remove columns from SQL output where all values are 0

  27. 27

    I am trying to add 2 values in a SQL Select Statement

  28. 28

    Filling in column based on column values

  29. 29

    mysql query: searching based on all values in column rather than one value in column

뜨겁다태그

보관