vote example

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

vote example

Post 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?
nahp
Forum Newbie
Posts: 7
Joined: Mon Apr 22, 2002 3:24 am

Post 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
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Semi-correct.... member1, member2, member3 are the names of the member. hits would be what would be counted up, total.
nahp
Forum Newbie
Posts: 7
Joined: Mon Apr 22, 2002 3:24 am

Post 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 )
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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?
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Hi

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

Try it.

:D
Mike
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Fair point, sometimes it is better to be explicit.

Mike
Post Reply