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 499535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:59:43+00:00 2026-05-13T05:59:43+00:00

sorry about the title, I’m not sure how to even describe this, which makes

  • 0

sorry about the title, I’m not sure how to even describe this, which makes it even harder to search for a solution.

I have a table which has many answers:

CREATE TABLE `answers` (
  `a_id` int(11) NOT NULL auto_increment,
  `p_id` int(11) NOT NULL default '0',
  `q_id` int(11) NOT NULL default '0',
  `user_id` int(11) NOT NULL default '0',
  `correct` int(1) NOT NULL default '0',
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`a_id`)
) ;

I need to select a q_id from a specific user_id and p_id where correct = 0, but only where a more recent row from the same user_id and p_id’s correct field is not 1.

I can group by id, but am unsure how to eliminate groups where the top correct != 0

Thanks in advance. I’ve found many helpful answers here and am looking forward to contributing where I can.

EDIT: Currently the query as stands, but takes 6 secs to execute:

From both your answers I have query that works, but takes 6 secs to execute!

SELECT a.q_id FROM answers a    
JOIN (SELECT b.q_id, MAX(b.a_id) as a_id, b.correct
        FROM answers b  
       WHERE b.correct = 1 
         AND b.user_id = 1 
         AND b.p_id = 22
    GROUP BY b.q_id) c ON a.q_id = c.q_id 
                      AND a.a_id > c.a_id
WHERE a.correct = 0 
  AND a.user_id = 1 
  AND a.p_id = 22  
LIMIT 1

With no JOIN they take .26 secs. and .45 secs to execute How can I make it more efficient?

Is there another way to select the most recent correct = 0 row where a prev correct = 1 row does not exist?

Thanks for all your help!

  • 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-05-13T05:59:44+00:00Added an answer on May 13, 2026 at 5:59 am

    This query seems to be working best. It’s my edited answer, updated with ordering the a.a_id and limiting the return to one.

      SELECT a.q_id 
          FROM user_q_answers a    
          JOIN (SELECT b.user_id,
                       b.popling_id, 
                 b.q_id,
                       MAX(b.a_id) as a_id
                  FROM user_q_answers b  
                 WHERE b.correct = 1 
              GROUP BY b.q_id, b.user_id) c ON a.q_id = c.q_id
                                           AND a.user_id = c.user_id
                                           AND a.a_id > c.a_id
         WHERE a.correct = 0 
           AND a.user_id = 101
           AND a.popling_id = 170  
        ORDER BY a.a_id DESC
        LIMIT 1
    

    Thanks very much to OMG Ponies and Nirmal for helping me through this. Sorry if answering my own question is bad form here, but I comprised my query based on what I learned from you both.

    Thanks again!!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry about the title I'm just not sure how to describe this one.
Sorry about the title, not sure how to describe without example. I trying to
Sorry about the title, I'm not exactly sure how to explain this with a
sorry about input mistakes, english its not my mother lang. I have this code
Sorry about the title — wasn’t sure how to word it. Basically I have
(sorry about the long title) I have a custom object Person, which in turn
I am sorry about the title as I am not sure whether my problem
Sorry about the title, don't know how to phrase this. I have my own
Firstly, I feel sorry about the title, I do not know how to describe
Sorry about the vague title, but I really don't know how to describe this

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.