Evgeny Davidof
Evgeny Davidof

Reputation: 77

How to fix error "CDbCommand ... SQLSTATE[42000]: Syntax error or access violation: 1064"

I get this error when I use sql request

$query = "SELECT SUM ".$fields."
                  FROM tbl_report all_months
                  ".$joins."
                  WHERE all_months.date > '$from_date' 
                        AND all_months.date < '$to_date' GROUP BY fio";

but with operator Distinct it works

$query = "SELECT DISTINCT ".$fields."
                  FROM tbl_report all_months
                  ".$joins."
                  WHERE all_months.date > '$from_date' 
                        AND all_months.date < '$to_date' GROUP BY fio";

Upvotes: 2

Views: 181

Answers (1)

Kevin
Kevin

Reputation: 41903

Use open and close parenthesis on mysql sum():

$query = "SELECT SUM (".$fields.") AS result_value

Note: As hanky has said in the comments, it might not work as expected if the value of $fields contains comma separated list of column names.

If it is indeed, then you need to use + instead of ,:

$fields = str_replace(',', '+', $fields);

Upvotes: 2

Related Questions