Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 9th, 2009, 05:20 AM
eciym eciym is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 1 eciym User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 45 sec
Reputation Power: 0
MySQL 5 - To left join or not?

hej, I was wondering if some of you wonderful people could give me a heads up on this,

I have been reading up and have summised that I need a LEFT JOIN

As i want it to list all the teams regardless of if they have points or not.

I have a table of teams with a UID and the team name.

then i have a table called points that stores the UID of the teams table as a foreign key, the number of points and each entry is timestamped for historical review later on.

what I want to do is display all the teams and the total number of points each team has acrued.

I get the sum of the teams with this
$query = "SELECT pofid, SUM(teampoints) FROM points GROUP BY pofid";

I presumed that being a relational database i wouldnt have to save the team name in the points table. Assumed it would be possible to get the team names also.

but how to get a query to display the team as well has foxed me.

I got this far
SELECT pofid, SUM(teampoints) AS totalpoints FROM points GROUP BY pofid LEFT JOIN teams ON points.pofid = teams.teid

was I even close? miles out? what did I miss? This seems like it should be so simple.

Am I barking up the wrong tree? should I be looking to create a totals feild in the teams table and update that with a total from the points table. to me that seems like it shouldnt be needed.

another question pops in to my head also;
How do I echo the aliis totalpoints in PHP?

Thanks in advance for any help with this.

Reply With Quote
  #2  
Old September 9th, 2009, 10:01 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
the group by goes at the end. any joins need to go right after the FROM SomeTable part because with the joins, you are still selecting the table(s) to select from. And yes, a left join is correct. an inner join (just join a table, not a left/right) will exclude any rows where there isn't a match in the joining table. a left join will select all rows from the first table, join the second table, but will still include any rows from the first table where there are no corresponding rows in the second table.

so:
Code:
SELECT pofid, SUM(teampoints) AS totalpoints FROM points LEFT JOIN teams ON points.pofid = teams.teid GROUP BY pofid

Reply With Quote
  #3  
Old September 10th, 2009, 10:32 AM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,753 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 8 h 15 m 54 sec
Reputation Power: 7
Couple of things. When joining you should always qualify your table names on the data you are collecting. Granted sometimes you can get away with not doing it if there is no overlap in the column names between the tables, but it is considered "good form" as well as a means of self documenting.

Also keep in mind that with Left Joins - the order of the tables in the listing matters. So you will do a full table scan on the `points` table - rather then the `teams` table - which may or may not be what you are expecting.

Code:
SELECT points.pofid AS ID,
  teams.name AS Team,
  SUM(teampoints) AS totalpoints 
FROM points 
  LEFT JOIN teams 
    ON points.pofid = teams.teid 
GROUP BY pofid


Lastly to access the aliased names from the result set with PHP is just like accessing any other column in the result set. I personally used associative arrays/objects and just called for the result by name.

Hope this helped.
__________________
life is a game.... Have fun
-----------------------------
http://www.phpwomen.org
strength in unity

PHPCommunity IRC
#phpc on freenode

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > MySQL 5 - To left join or not?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek