close ad
WARNING: Do Not Install the DREAMWEAVER CC 2017 or 2018 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Data structure advice

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

iainmacdonald331081

Data structure advice

Just looking for a bit of advice as to how to best go set up my database structure.

I currently have the following tables:

lodges

LodgeID (PK)
Lodge
etc

scores

ScoreID (PK)
Score
CategoryID (FK)
LodgeID (FK)
etc

categoriesFull

CategoryID (PK)
Category
etc

countries

CountryID (PK)
Country

So any lodge can have multiple scores, in multiple categories.

And I can currently produce a list of lodges for any given category, in a particular country which lists the lodge, the number of votes in the category, and the average score in the for that category, using:

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

So far, so good.

What I'm looking to do now is allow create two new fields, 'rank', and 'finalist'. So the plan is to use an update multiple record on those pages, with a tick box for 'finalist' and a text / integer field for 'rank'.

So someone can view all the lodges in a list and just tick the boxes for the ones they want to be a finalist, enter the rank of 1-10 for the top 10 ,and click update to update all the records. (Or if it's a new table it would be an insert multiple records)

However, I am not sure which table it would be best to put these in. They're kind of related to the scores, but really apply to the lodges, i.e. any given lodge will be deemed to be a finalist in a particular category, and may be ranked in any category.

At the moment I'm thinking I just need an extra table:

finalists

FinalistID (PK)
Finalist (yes / no)
Rank
LodgeID (FK)
CategoryID (FK)
CountryID (FK)

Hope that makes sense - any advice on this would be appreciated before I start down the wrong path.

Thank you!

Sign in to reply to this post

Jason ByrnesWebAssist

That looks like it should work for what you are trying to do.

Sign in to reply to this post

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.

Sign in to reply to this post

Jason ByrnesWebAssist

I would need a dump of the database in order to troubleshoot.

Sign in to reply to this post

iainmacdonald331081

Thanks Jason - see below.

Sign in to reply to this post

Jason ByrnesWebAssist

nowhere do you have a join statement for the categoriesFull, you can use the categoriesFull.CategoryID as a comparison:

AND nominations.CategoriesFullID = categoriesFull.CategoryID


whithout first joining it. try using:

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

Sign in to reply to this post

iainmacdonald331081

Thanks Jason - that all looks like it works, although I did notice two odd records.

There should I think be 47 matches on that example above but it returns 49.

The two extra ones are LodgeID 21 and 820.

If you query:

SELECT * FROM `scores` WHERE LodgeID = 21 OR LodgeID = 820 ORDER BY `ScoreID` ASC

You can see that neither have a score in category 7, but do show up using the SQL above.

Which is odd as the other 47 all look correct.

Sign in to reply to this post

Jason ByrnesWebAssist

using the database dump you sent to me and the SQL above, I Get 19 records, not 47 or 49.

Sign in to reply to this post

iainmacdonald331081

Sorry Jason - I added all the records to the nominations table for that category in that country.

Have attached a new database dump below.

Thanks again.

Sign in to reply to this post

Jason ByrnesWebAssist

i think you might need to remove the group by

Sign in to reply to this post
loading

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