Jump to content

Querying multple tables


PHP_Idiot

Recommended Posts

Hi Firstly thanks for even reading this, secondly I'm new to mysql so this might sem a dumb question to you pros out there.

 

I have 4 tables

Players

MembershipNo, FirstName, LastName, Town, Email

Venue

VenueID, VenueName, Address1, Address2, Town, County, Postcode, TelNo, FaxNo, Email, Web, GameNight1, GameTime1, GameNight2, GameTime2, ContactFirstName, ContactLastName, ContactMobile

Results

ResultID, VenueID, MembershipNo, Position

Position

Position, Points

 

The first Column Names in each above is the PrimaryKey.

 

What I need to do is create queries that will:

[*]Search all points earnt for a given MembershipNo

[*]View Finishing poisitions for all players (membershipNo, FirstName, LastName) for a given Venue on a given Date

[*]Search total Points earnt for all Players (creating a leader Board)

 

I understand how to do a basic Select to pull all or specific data from a table and sort it, but these cross tables, and although I have search for examples and found some I can't get any to actually work.

 

If anyone could show me how I wold be eternally grateful.

Many Thanks

Link to comment
Share on other sites

Hi shlumph,

I've been looking at both UNION and JOIN, but I think your right JOIN is the way to go,

I've never used it before and although I can get some success in joining 2 tables I'm struggling to join more than 2.

 

I'm especially having difficulty with creating a SUM(Points) to total a players points across a particular venue.

 

I would be really gratefull If you could give me an example based on my tables, it would help me see where I'm going wrong and also help me learn how to use it in other situations I need to solve.

(I know it's a big ask, but this is a giant leap outside my comfort zone in mysql!)

 

Many Thanks

Link to comment
Share on other sites

Sure- this is something that should push you in the right direction:

SELECT p.*, v.*, r.*, pos.* FROM Players p
LEFT JOIN Results r ON p.MembershipNo = r.MembershipNo
LEFT JOIN Venue v ON r.VenuID = v.VenueID
LEFT JOIN Position pos ON pos.Position = r.Position 
WHERE MembershipNo = 112

 

This will return all the results, venues, and position rows from whoever has membership number 112. Notice how I connect the tables with primary/foreign keys.

Link to comment
Share on other sites

Thats awesome thanks, it gives me a much clearer idea as to what I need to try to do, unfortunately you example throws up the following error:

#1052 - Column 'MembershipNo' in where clause is ambiguous

Is that because both collumn names are identical? I might try changing them fractionally to see if that helps.

 

Some home work for me to do tomorrow :-)

Thanks a million shlumph

Link to comment
Share on other sites

LOL ;D

 

That makes perfect sense, but I changed the name, which also worked, however, I prefer the 'correct' method as you described so will change it back :)

 

My next job it to work out how to put a sum() in there as well!!

 

Thanks a lot for all you help, it's really appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.