close ad
Databridge V2 with MySQLi support IS Now Available!
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

Data structure advice

Thread began 9/02/2014 12:16 pm by iainmacdonald331081 | Last modified 9/05/2014 9:23 am by iainmacdonald331081 | 1006 views | 14 replies

iainmacdonald331081

Thanks Jason - I actually had another table, nominations, which I can use. Basically each lodge can receive multiple votes / scores for any given category, for any given country. But would only have one nomination in any given category, for any given country. i.e. if it gets any votes in a category, for a country, it then gets a new nomination record.

And the idea is to use the that nominations record for judges to then flag any as finalists and rank the top 10.

I haven't been able to get the SQL for the query right though - presumably because of wrong JOINs and not being quite sure how to join on multiple criteria.

The tables are as above, plus:

nominations

NominationID (PK)
LodgeID (FK)
CategoriesFullID (FK) (--to categoriesFull.CategoryID)
SW_finalist
SW_rank

So if I have the following:

lodges
LodgeID, Lodge
798, Borana Lodge

scores
ScoreID, LodgeID, CategoryID, Score
1001, 798, 7, 3
1002, 798, 7, 6
1003, 798, 7, 9

categoriesFull
CategoryID
7

countries
CountryID
3

nominations
NominationID, LodgeID, CategoryID, SW_finalist, SW_rank
1234, 798, 7, Yes, 2

I'd like to be able to output

LodgeID, Lodge, CategoryID, CountryID, ScoreCount, AverageScore, NominationID, SW_finalist, SW_rank
798, Borana Lodge, 7, 3, 3, 6, 1234, Yes, 2

This is what I have before introducing the nominations table:

SELECT ScoreID, COUNT(ScoreID) as scoreCount, AVG(Score) as AverageScore, lodges.LodgeID, Lodge, Country, lodges.CountryID, CategoryID, SW_finalist, SW_rank FROM lodges INNER JOIN countries ON lodges.CountryID = countries.CountryID INNER JOIN nominations ON lodges.LodgeID=nominations.LodgeID INNER JOIN scores ON lodges.LodgeID = scores.LodgeID WHERE lodges.CountryID = 3 AND CategoryID='7' GROUP BY Lodge

I presume the join on nominations needs to be also be on categoriesFull.CategoryID, but when I try something like

SELECT ScoreID, COUNT(ScoreID) as scoreCount, AVG(Score) as AverageScore, lodges.LodgeID, Lodge, Country, lodges.CountryID, CategoryID, SW_finalist, SW_rank FROM lodges INNER JOIN countries ON lodges.CountryID = countries.CountryID LEFT OUTER JOIN nominations ON lodges.LodgeID=nominations.LodgeID AND nominations.CategoriesFullID = categoriesFull.CategoryID INNER JOIN scores ON lodges.LodgeID = scores.LodgeID WHERE lodges.CountryID = 3 AND CategoryID='7' GROUP BY Lodge

I get an error about categoriesFull.CategoryID being an unknown column.

I think all the tables have the IDs they need, but any help with the right joins to get it to work would be much appreciated.

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...