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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:29:26+00:00 2026-06-18T00:29:26+00:00

I am developing a Online Exam Application where I have a question master table

  • 0

I am developing a Online Exam Application where I have a question master table which contains a field ( name : qtype ) containing the data for the type of question. A question type can be either of following three types :

  1. single
  2. multiple
  3. desc

The application is generating a random question paper from this master table by using the following query :

select * from mst_question where test_id = 1 ORDER BY RAND() LIMIT 25

This generates a random questionnaire of 25 questions for my Online Exam Application.

So far Its working good…

Now, I need to implement a feature in my application where the 25 randomly generated questions ( or whatever number – this will depend on the test id ) will always have a FIXED mix of the different types of questions available in the master question table ( mst_question ) for every randomly generated questionnaire set.

Say, if master question table is having 108 questions for a particular test id and all the three types of questions are in the db for this test , it will provide same number of different types questions for each random query !! I have written the following sql query to find out the percentage of each type of question in the master question table.

So far i have tried this and came up with this sql query :

select qtype,count(*) as qtypetotal,(select count(*) from mst_question where test_id = 1) as totalqtype,round((count(*)/(select count(*) from mst_question where test_id = 1)*100),2) as qtypepercentage from mst_question where test_id = 1 group by qtype

The output of above query is :

qtype       qtypetotal  totalqtype  qtypepercentage  
desc        24      108     22.22 %  
multiple    34      108     31.48 %  
single      50      108     46.30 %  

I need to form a sql query which will give me 25 randomly generated questions where 22.22% of 25 questions should be desc type , 31.48 % of 25 questions should be multiple type and remaining 46.30 % of 25 questions should be single type.

I am stuck ……. Pls advise…

Thanks in advance 🙂

Thanks @MikeBrant…. I have generated the dynamic sql which is definitely the way i wanted… just one issue now…. if i execute this sql query :

select qtype,round(25*(count(*)/(select count(*) from mst_question where test_id = 1))) as numquests from mst_question where test_id = 1 group by qtype

I am getting the following results :

“desc” “6”
“multiple” “8”
“single” “12”

and based on the above query i generate this dynamic query :

$dynamicsql[] =  "(SELECT * FROM mst_question WHERE test_id = 1 AND qtype = '".trim($rstype->qtype)."' ORDER BY RAND() LIMIT ".$rstype->numquests.")";

$finalsql = implode(" UNION ALL ",$dynamicsql)." ORDER BY RAND()";

I want to generate a total of 25 random questions but the sum of these qtypes is 26 !!! and i am getting a question extra 🙁

  • 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-18T00:29:27+00:00Added an answer on June 18, 2026 at 12:29 am

    I would probably just use a UNION here:

        (SELECT * FROM mst_question
        WHERE test_id = 1 AND qtype = 'desc'
        ORDER BY RAND() LIMIT X)
    UNION ALL
        (SELECT * FROM mst_question
        WHERE test_id = 1 AND qtype = 'multiple'
        ORDER BY RAND() LIMIT Y)
    UNION ALL
        (SELECT * FROM mst_question
        WHERE test_id = 1 AND qtype = 'single'
        ORDER BY RAND() LIMIT Z)
    ORDER BY RAND()
    

    You can use a query like you posed on your original question to get the values for X, Y, and Z to be used.

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

Sidebar

Related Questions

As a follow up to this question: Developing a online exam application, how do
I'm developing an online website (using Django and Mysql). I have a Tests table
I am developing an online application in which I need to take a printout
I'm developing an application which can buffer online streaming radio to a file say,
I'm developing an online gallery with voting and have a separate table for pictures
I'm developing an online application that will have many outside users. As for now
I am developing an online exam using JSP/Servlets which is nearing completion. I'm planning
I'm developing an online application for education research, where I frequently have the need
I'm developing online examination system in php-mysql. That exam will consist of multiple-choice questions;
Developing for Android 2.3, I have a question regarding layouts. I use a vertival

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.