E
E
Evgeny-app2014-09-02 06:05:10
MySQL
Evgeny-app, 2014-09-02 06:05:10

How to display the sum of sql values ​​with a query if the given one goes into a variable?

There is a query that displays all non-repeating values ​​and it works:

$query = "SELECT DISTINCT ".$fields."
                      FROM tbl_report all_months
                      ".$joins."
                      WHERE all_months.date > '".$year."-01-01 00:00:00' AND all_months.date < '".$year."-12-31 24:59:59'";

but if I want to sum these values ​​then you give a syntax error (CDbCommand failed to execute SQL query: SQLSTATE[42000]: Syntax error or access violation: 1064)
$query = "SELECT SUM ".$fields."
                      FROM tbl_report all_months
                      ".$joins."
                      WHERE all_months.date > '".$year."-01-01 00:00:00' AND all_months.date < '".$year."-12-31 24:59:59'";

Why is that??

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2014-09-02
@DmitriyEntelis

SUM is an aggregation function and can only take 1 argument.
www.tizag.com/mysqlTutorial/mysqlsum.php
If you need to sum columns within a single tuple, write explicitly:
PS
If you consider some kind of statistics, then I doubt that distinct is needed in the first request, most likely the request was made incorrectly

A
andymitrich, 2014-09-02
@andymitrich

And for starters, open the MySQL manual, read, for example, how to use SUM () correctly, and then, believe me, everything will become clear.

F
falsebyte, 2014-09-02
@falsebyte

"SELECT SUM (".$fields.")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question