Page 1 of 1

vote example

Posted: Sun Apr 28, 2002 11:07 pm
by phice
Lets say I've got a col in mysql, that is entitled hits. Now, there are 3 members in that certain table. Each time a visitor visits member1 page, it adds +1 to the # thats already in member1's col, entitled "hits". Now, member1 has 82 hits, member2 has 89 hits, and member3 has 49 hits. How would I add up all of the portions of hits? any ideas? Anyone need any furthur explenation?

Posted: Mon Apr 29, 2002 1:28 am
by nahp
correct me if i m wrong:
member1, member2 and member3 are 3 different columns in one table
and you want the sum of theses columns
so what u r lookin for might be:

select ( member1 + member2 + member3 ) as totalHits from hits

Posted: Wed May 01, 2002 11:54 pm
by phice
Semi-correct.... member1, member2, member3 are the names of the member. hits would be what would be counted up, total.

Posted: Thu May 02, 2002 3:01 am
by nahp
keeping on correcting me if i m still wrong,
this might be more useful:
select sum( hits ) from t_table
with this u ll get the total hits

if u just want for specific members:
select sum( hits ) from t_table where member in( member1 , member2 , member3 )

Posted: Thu May 02, 2002 9:31 pm
by phice
Sounds good..


And, then in $row, I can set $hits = $row['hits'];, and $hits will be the total of all of the hits?

Posted: Fri May 03, 2002 3:33 am
by mikeq
And, then in $row, I can set $hits = $row['hits'];, and $hits will be the total of all of the hits?
That wouldn't work because the field is no longer called 'hits' it is called 'sum(hits)', you could give the column an alias

Code: Select all

select sum(hits) TOTAL_HITS from t_table
then in $row, set $hits = $row['TOTAL_HITS'];

Mike

Posted: Fri May 03, 2002 5:41 am
by twigletmac
mikeq wrote:

Code: Select all

select sum(hits) TOTAL_HITS from t_table
You need to add an AS keyword in there

Code: Select all

SELECT SUM(hits) AS TOTAL_HITS FROM t_table
so that TOTAL_HITS will be an alias for SUM(hits).

Probably just a typo :) ,
Mac

Posted: Fri May 03, 2002 5:44 am
by mikeq
Hi

Nope not a typo, you don't need to add the AS keyword.

Try it.

:D
Mike

Posted: Fri May 03, 2002 6:19 am
by twigletmac
mikeq wrote:you don't need to add the AS keyword.
:oops: Sorry hadn't noticed that the AS was optional.

However, it might be a good idea to use it anyway so that the SQL statement is easier to read . If I saw something like that in someone else's code I'd probably assume that they'd missed out a comma or something and muck up the code :? .

Mac

Posted: Fri May 03, 2002 8:34 am
by mikeq
Fair point, sometimes it is better to be explicit.

Mike