Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7747443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:31:49+00:00 2026-06-01T10:31:49+00:00

I am using the Moodle database to query the latest forum posts made by

  • 0

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:

SELECT l.time AS TimeofPost, l.action as Action, usr.id as UserID, 
       usr.firstname,usr.lastname, c.id as CourseID , c.fullname, 
       c.idnumber, fd.name, fd.timemodified as CreatedOn, fp.created, 
       fp.modified, fp.subject, fp.message

FROM mdl_forum_posts fp

LEFT JOIN mdl_forum_discussions fd ON fp.discussion = fd.id
LEFT JOIN mdl_user usr ON fp.userid = usr.id
LEFT JOIN mdl_log l ON usr.id = l.userid
LEFT JOIN mdl_user_enrolments ue ON usr.id = ue.userid
LEFT JOIN mdl_enrol e ON ue.enrolid = e.id
LEFT JOIN mdl_course c ON e.courseid = c.id

WHERE (action = 'add post' OR action = 'add discussion')    

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:31:51+00:00Added an answer on June 1, 2026 at 10:31 am

    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.

        SELECT fp.id, 
               usr.id as UserID, 
               usr.firstname,
               usr.lastname, 
               c.id as CourseID, 
               c.fullname, 
               c.idnumber, 
               fd.name, 
               fd.timemodified as DiscussionCreatedOn, 
               fp.created AS TimeofPost, 
               fp.modified, 
               fp.subject, 
               fp.message
          FROM mdl_forum_posts fp
    INNER JOIN mdl_forum_discussions fd ON fp.discussion = fd.id
    INNER JOIN mdl_forum f ON f.id = fd.forum
    INNER JOIN mdl_course c ON f.course = c.id 
    INNER JOIN mdl_user usr ON fp.userid = usr.id
         WHERE EXISTS (SELECT 1 
                         FROM mdl_user_enrolments ue
                   INNER JOIN mdl_enrol e ON ue.enrolid = e.id 
                        WHERE usr.id = ue.userid 
                          AND e.courseid = f.course)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using moodle but I need a better forum, so I was thinking that
Here in srilanka we don't have much internet facilities, we are using Moodle in
I am using Moodle 1.9 Paypal_enrolment plugin so that to access the course student
I am using moodle and I change location of my web site, but I
I am trying to install Moodle 1.9 using XAMPP on a virtual machine running
I am working on Moodle 2.2.1 and have created dynamic.csv files using certain queries.
I have my moodle setup using this guide ( Ubuntu 11.04 ) http://docs.moodle.org/dev/Install_Moodle_On_Ubuntu_with_Nginx/PHP-fpm I
With Moodle, when you are the teacher, you can share files with your students.
I'm planning on installing Moodle in a server (using an Oracle 10.2 DB and
i am using Moodle and joomla , and i use joomdle , but i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.