Re: Fw: newbie query query
| From: | John McKown | Date: | Sun, 17 Sep 2000 01:19:45 +0000 |
| Subject: | Re: Fw: newbie query query | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-2933@lists.php.net to get a copy of this message | ||
On Sat, 16 Sep 2000, Scott M Wagner wrote:
<snip>
> Hello!
> Can anyone tell me why the 'sum(goal_1) as Total' command will not
> execute in the following php script? Thanks very much.
>
> $sql = "
> select id_number, date, goal_1, goal_2, goal_3, goal_4, wu,
> sum(goal_1)as Total
> from $table_name
> where date >= '2000-09-08' and date <= '2000-09-14'
> order by date
> ";
I'm not real familiar with MySQL because I use PostgreSQL. But I think
that all SQL compliant databases will fail that query. The reason is that
when you use an aggregate function (such as sum()) in the SELECT clause,
then you must use an GROUP BY for all the columns in the SELECT which are
not aggregate functions. In your case, I think it will work if you
add a "group by" clause such as:
"group by date, id_number, goal_1, goal_2, goal_3, goal_4, wu"
This clause goes after the WHERE clause and before the ORDER BY clause.
I hope this is of some help to you,
John