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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:34:14+00:00 2026-05-16T20:34:14+00:00

I am developing a system which selects questions from a database to generate a

  • 0

I am developing a system which selects questions from a database to generate a test/exam.

Each question has a set ‘question type’ (‘q_type‘). I have a database of questions and need to select 4 questions, each with a different ‘q_type’.

The basic query to select 4 random questions at the moment is:

SELECT * FROM questions ORDER BY RAND() LIMIT 0,4

This obviously does not take into account the fact that each question should have a different ‘q_type’.

I would like to be able to do something that follows this logic (i need something to fill in the square brackets):

SELECT * FROM questions WHERE ['q_type' is DISTINCT] ORDER BY RAND() LIMIT 0,4

I have tried using GROUP BY ‘q_type’, but that simply gives the first question for each ‘q_type’, not a different question of that type each time.

Any help would be great, as I am completely stumped at the moment (I am working with a overblown PHP loop which simply queries the DB 4 times, with an updated WHERE ‘q_type’!=XX each time).

Thanks for any 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-16T20:34:14+00:00Added an answer on May 16, 2026 at 8:34 pm

    I don’t think there’s an easy way to do this with simple queries. You could probably do it with a mysql stored procedure, or to save some development time do it in php.

    If the pool of questions isn’t something extremely large, and this isn’t something that happens too frequently, you should be ok with the 4 separate queries (one for each q_type), or even with getting the entire question set into php and then playing around with it to get 4 random questions, one in each q_type.

    With the following (untested) code you’ll be getting all questions ordered randomly and loop through them to get the first question for each q_type, then stop. It’s not the most elegant solution but, again, for small enough data sets and frequency it should be ok.

    $questions = array() ;
    $res = mysql_query('SELECT * FROM questions ORDER BY RAND()') ;
    while ($row = mysql_fetch_assoc($res)) {
    
        //if we have all 4, stop
        if (count($questions) == 4) {
            break ;
        }
    
        $currType = $row['q_type'] ;
        $currQuestion = $row['questionTitle'] ;
    
        if (isset($questions[$currType])) {
            //already have it, continue to the next
            continue ;
        }
    
        $questions[$currType] = $currQuestion ;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a PHP-based login system. Each user has an ID(a number) and
My table has a timestamp column named RowVer which LINQ maps to type System.Data.Linq.Binary.
I'm developing an embedded system which currently boots linux with console output on serial
I am developing a custom control derived from System.Windows.Controls.ContentControl. In the controls default template
I'm developing a set of applications that work together to create a system for
I am currently developing on an advertising system, which have been running just fine
I am developing a system as an aid to musicians performing transcription. The aim
When developing on a system with dual monitors, I like to make the most
I'm developing an operating system and rather than programming the kernel, I'm designing the
We are developing a considerably big application using Ruby on Rails framework (CRM system)

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.