Jump to content

[SOLVED] GROUP BY or DISTINCT?


l3asturd

Recommended Posts

ok, I don't have any code yet, my module is a concept until I can figure out if it's possible. So here's what I have:

 

A table that has user names, golf courses, and scores at each course. There are multiple entries for each course but different scores. OK here's an example table:

 

    Name        Score      Course

-------------------------------------

John              50          Visalia

John              49          Visalia

Rick              48          Visalia

John              52          Tulare

Rick              77          Hanford

Rick              50          Tulare

 

 

OK, now, on my stats page, I wanted users to be able to see their score averages for each course. I know how to pull the data from SQL and GROUP BY course. I know how to AVG(Score) AS TotalAverage.  Problem is, how can I average scores for each course AND for each player at the same time.

 

John's average score at Visalia is 50

John's average score at Tulare is 52

 

See what I'm getting at. I tried something like:

 

SELECT * from TABLE WHERE name=John GROUP BY course AVG(score) AS courseaverage

 

but this is bad syntax, and wouldn't result in the data I'm looking for. Any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/74678-solved-group-by-or-distinct/
Share on other sites

  • 3 months later...

You almost never want distinct...

Why do you almost never want distinct? I'm trying to find out which is best (performance-wise) - group by or distinct. seems to be a big debate on this with most people saying they are both identical. your comment suggests you think there is a difference.

i.e.

SELECT DISTINCT id FROM phrase WHERE campaign LIKE 'proper%'

versus

SELECT id FROM phrase WHERE campaign LIKE 'proper%' GROUP BY advert_id

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.