Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
phice
Moderator
Posts: 1416 Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:
Post
by phice » Sun Apr 28, 2002 11:07 pm
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 » Mon Apr 29, 2002 1:28 am
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
phice
Moderator
Posts: 1416 Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:
Post
by phice » Wed May 01, 2002 11:54 pm
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 » Thu May 02, 2002 3:01 am
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 )
phice
Moderator
Posts: 1416 Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:
Post
by phice » Thu May 02, 2002 9:31 pm
Sounds good..
And, then in $row, I can set $hits = $row['hits'];, and $hits will be the total of all of the hits?
mikeq
Forum Regular
Posts: 512 Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland
Post
by mikeq » Fri May 03, 2002 3:33 am
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
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri May 03, 2002 5:41 am
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
mikeq
Forum Regular
Posts: 512 Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland
Post
by mikeq » Fri May 03, 2002 5:44 am
Hi
Nope not a typo, you don't
need to add the AS keyword.
Try it.
Mike
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri May 03, 2002 6:19 am
mikeq wrote: you don't need to add the AS keyword.
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
mikeq
Forum Regular
Posts: 512 Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland
Post
by mikeq » Fri May 03, 2002 8:34 am
Fair point, sometimes it is better to be explicit.
Mike