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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:01:49+00:00 2026-06-16T20:01:49+00:00

how to (even logically) random at least one e.g. Id from database which has

  • 0

how to (even logically) random at least one e.g. Id from database which has specified column value?

I mean I have in database table id, question id, answer text value and bool (if answer is correct) columns and I want to get e.g. three answers Ids, that (at least) one of them is correct.

Many thanks for your help in advice!

  • 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-16T20:01:50+00:00Added an answer on June 16, 2026 at 8:01 pm

    So you want to make a multiple choice thing, and display x out of y answers where, out of those y, c are correct, and w are wrong (c+w=y).

    I would suggest using 2 queries, first get a random correct answer, and then get x-1 incorrect answers. I would recommend against using two or more correct answers, as it will just confuse the user.

    To get the correct answer, you could do:

    SELECT TOP 1 ID 
    FROM Answers 
    WHERE QuestionID =@QuestionID -- parameter supplied from code
      AND IsCorrect = 1 
    ORDER BY NEWID()
    

    After that do

    SELECT TOP 3 ID  -- assuming the X = 4 value is hardcoded in the application
    FROM Answers 
    WHERE QuestionID =@QuestionID -- parameter supplied from code
      AND IsCorrect = 0
    ORDER BY NEWID()
    

    If you do want to include any other answer, including a correct one, you could do

    SELECT TOP 3 ID
    FROM Answers 
    WHERE QuestionID = @QuestionID
      AND ID != @ID -- where ID is the ID of the question from the first query
    ORDER BY NEWID()
    

    Since the questions won’t have a large answer set (I guess not more that 10 or 20), using NEWID() on each row will not be very bad for performance.

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

Sidebar

Related Questions

Even though I have a fermi card(gtx 560) I get this error on VS2010:
I have trouble understanding what the following code means (and partly, why it even
Is there a method of logically merging multiple dictionaries if they have common strings
I'm trying to have logical separators keeping all my properties logically separate, but so
Ok so I'm not even sure how to title this post but I have
So I've got this piece of code which, logically should work but Entity Framework
I have created a TableRow layout model which I'm using as a template for
I don't have much experience with SQLAlchemy and I have a problem, which I
I'm having an asbolute nightmare dealing with an array of numbers which has the
We develop applications for the Flash platform, which have LOTS of run-time loaded assets

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.