First off, I am not very good with MySQL yet, so go easy on me. I am not even sure how to word my question.
I have two tables; one table contains information about links, the other ratings of each link made by users. Essentially, I just need to be able to order the links table by rating average.
TABLES:
links
- link_id PK int autoincrement
- url varchar
- title varchar
- description text
- rating decimal
link_ratings
- link_rating_id PK int autoincrement
- link_id FK int
- user_id FK int
- rating decimal
What I need is when someone adds a link rating to a link (new link_ratings entry) or updates their current rating, all of the ratings for that link are averaged and updated on the rating column in links. OR Can I scrap the rating column in the links table entirely and use a JOIN?
If so, how would I go about doing the JOIN. If not, how do I make TRIGGER that would accomplish this for me?
I haven’t been able to find much information, most likely due to my lack of SQL knowledge, of how to even start this.
Any help is appreciated. Thanks.
You don’t need to persist the
ratingaverage in thelinkstable. I am calculating it below, and this should perform reasonably well: