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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:58:27+00:00 2026-06-17T09:58:27+00:00

I have no idea if this is a SQL functionality or not… In short:

  • 0

I have no idea if this is a SQL functionality or not… In short: I want rows from a table which id can’t be found in a array/comma-seperated-list in another table. More info below:

The setup is two tables; a user-table and a questions-table. The user-table has a column called answeredQuestions, a comma-seperated list, which contains the questions-ids the user has answered.

I am trying to get 4 random questions, where each one hasn’t been answered yet.

How I get 4 random with SQL query

SELECT * FROM questions WHERE id >= RAND() * (SELECT MAX(id) FROM questions) LIMIT 4

But this will just return 4 random questions, without concidering wether or not the user has had that question before.

If I should do it in Javascript, it would look like this

var questions = [1,2,3,4,5,6,7,8,9,10,11,12];
var answeredQuestions = [3,5,9,12];
var sqlReturn = [];

for (var i=0;i<4;i++) { 
  var randNo = 0;

  while (randNo == 0 || sqlReturn.indexOf(randNo) > -1 || answeredQuestions.indexOf(randNo) > -1) {
    randNo = Math.floor((Math.random()*questions.length)+1);
  }

  sqlReturn[i] = randNo;
}

document.write(sqlReturn);
  • 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-17T09:58:29+00:00Added an answer on June 17, 2026 at 9:58 am

    Don’t really like your solution. You are making things a lot harder for yourself with your underlying database design.

    You have two tables, one representing users and another representing questions. What you really need is a table linking the two concepts, something like user-questions.

    Suggested design:-

    create table `user-questions`
    (
       user_id int,
       question_id int,
       answered datetime
    )
    

    Suggested approach for recording answers.

    Every time your user answers a question, whack a row into user-questions to signify the fact that a user has answered the question.

    Under this structure, solving your specific problem, finding questions that haven’t been answered yet, becomes trivial.

    -- Find a question that hasn't been answered by user id 22.
    SELECT
      q.* 
    FROM 
      `questions`
    LEFT OUTER JOIN `user-questions` uq
    ON q.question_id = uq.question_id
    -- Just a sample user ID
    AND uq.user_id = 22
    WHERE
      uq.question_id IS NULL
    

    I don’t play day to day with MySQL, so please feel free to correct any typos, SO’ers. The approach is sound, though.

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

Sidebar

Related Questions

Have someone idea, why this SQL return error on create table accounts? 1005 -
I have this idea that using SQL VIEWS to abstract simple database computations (such
SQL Server 2008. I have this very large query which has a high cost
I have no idea how to convert this SQL statement to LINQ that uses
I have this SQL Server 2008 UDT: CREATE TYPE [dbo].[IdentityType] AS TABLE( [Id] [int]
I have a java.sql.Clob object which I populate from an Oracle query and then
So I have this idea for a RubyGem that I think would be an
I have no idea why this is happening. The error I get is the
I have no idea if this question fit to SO, but if it doesn't
I have no idea if this is possible, but if it is, what would

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.