Skip to content
Advertisement

Why am I getting duplicate results in my query?

I am using the Moodle database to query the latest forum posts made by students and teachers for the relevant course they are enrolled in.

Here are the tables I need to join and an explanation in what they hold:

mdl_forum_posts = “all posts are stored in this table”
mdl_forum_discussions = “forums are composed as discussions”
mdl_user = “information stored about users”
mdl_log = “activity of every user of the system be it, log in – adding a post”
mdl_user_enrolments = “users participating in courses”
mdl_enrol = “instances of enrolment plugins used in mdl_course”
mdl_course = “courses available in the system”

Here is my query:

The problem I have is that I am getting duplicate results.

Advertisement

Answer

No need to use the logs table – the fact that a post has been added and has a timestamp is all you need. Also, you don’t really want to use LEFT JOIN unless you expect null values, which should never be the case here. I’ve left in the link to the enrolment table so that if any students are unenrolled, they won’t show up, but this isn’t really necessary.

You are getting duplicates because there are multiple matches for each forum post e.g. enrol table has many instances for each course. This query makes sure you just get stuff where there is one.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement