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

  • SEARCH
  • Home
  • 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 8391939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:23:20+00:00 2026-06-09T19:23:20+00:00

I’m trying to return the first created record, and all but the first created

  • 0

I’m trying to return the first created record, and all but the first created record, for all individual users given certain constraints. My SQL is broken, but I can’t for the life of me figure out where/why.

Object Model

I have 4 models of interest: User, Course, Question, and QuestionRecord.

Everytime a User answers a Question, a QuestionRecord is generated. QuestionRecord is a many-to-one with User, Course, and Question. (Is that cardinality correct? Each QuestionRecord has a foreign key to exactly one User, one Course, and one Question)

In Ruby:

class QuestionRecord < ActiveRecord::Base

  belongs_to :user
  belongs_to :course
  belongs_to :question
…
end

Desired Outcome #1: First Record

What I’d like is to return the first question record for each user, given a course and a question. Put another way, if Jane, and Bill have answered the question twice, John hasn’t attempted it, and Pete has answered it 7 times, I want 3 records returned, the first answer attempt, by creation date, from Jane, Bill, and Pete.

I could do this in the code, but want it in SQL for efficiency.

This is what I have:

SELECT qr.id 
FROM users u 
INNER JOIN question_records qr 
ON u.id = 
  (SELECT x.user_id 
   FROM question_records x 
   WHERE x.course_id = #{course.id} 
   AND x.question_id = #{question.id} 
   AND x.user_id = u.id 
   ORDER BY created_at ASC 
   LIMIT 1 )

This query just runs for 40 minutes or so, and doesn’t actually return anything. I ran it with a subset of 50 records, and had the weird issue that all the returned qr.id results were “1”

Desired Result #2: All but the first record

Now here, I want all records for all users except the first record. My thought was that, in essence, this is the same query, except that I want all records, and I want to offset the returned index by one:

SELECT qr.id 
FROM users u 
INNER JOIN question_records qr 
ON u.id = 
  (SELECT x.user_id 
   FROM question_records x 
   WHERE x.course_id = #{course.id} 
   AND x.question_id = #{question.id} 
   AND x.user_id = u.id 
   ORDER BY created_at ASC 
   OFFSET 1 )

However, it’s obvious that if the first query doesn’t work, the second won’t.

Coda

Does anyone have any guidance on this query? It’d be nice to have it as efficient as possible, so I could index question_records on [course_id, question_id] or whatever it takes. I’m also assuming that I’m missing a possibility to use “GROUP BY x.user_id”, but wasn’t sure how to add it without adding more gum in the works.

For the record, the database we’re using is MySQL.

  • 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-09T19:23:22+00:00Added an answer on June 9, 2026 at 7:23 pm

    For #1, if your ID values are sequentially issued:

    SELECT     MIN(qr.id) AS FirstQRID
    FROM         users AS u INNER JOIN
                          question_records AS qr ON qr.user_id = u.id
    WHERE     (qr.course_id = #{course.id}) AND (qr.question_id = #{questionid})
    GROUP BY u.id
    

    Then, for #2:

    SELECT qr.id
    FROM         questions AS qr2
    WHERE      (qr2.course_id = #{course.id}) AND (qr2.question_id = #{questionid}) AND
               (NOT qr.id = (SELECT     MIN(qr.id) AS FirstQRID
                             FROM         users AS u INNER JOIN
                                                   question_records AS qr ON qr.user_id = u.id
                             WHERE     (qr.course_id = qr2.course_id) AND (qr.question_id = qr2.quesion_id) AND
                                       (qr.user_id = qr2.user_id)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.