0

I have the following query to select question ids in table_question:

$sql = "SELECT s.question_id
        FROM table_question s
        WHERE s.chapter_id = $chapter_id
        AND s.publish_status = ".PUBLISHED_ACTIVE." AND (
        s.format_type_id = ".QST_TYPE_OBJECTIVE." OR (s.format_type_id = ".QST_TYPE_SHORT_ANSWER." AND s.question_id < 200000 AND s.publish_status = ".PUBLISHED_ACTIVE."))
        ORDER BY RAND() LIMIT 5";

$obj_question_ids = DB::connection('mysql_question')->select(DB::raw($sql));

In my database, I got 133010 of objective questions and 15031 subjective questions. Now, how I can make sure the ratio of my question selection will be

70% Objectives questions and 30% questions will be structure questions?

Nere
  • 4,097
  • 5
  • 31
  • 71

1 Answers1

2

This might work

Let's try with parenthesis

usage of union

   (SELECT s.question_id
            FROM table_question s
            WHERE s.chapter_id = $chapter_id
            AND s.publish_status = ".PUBLISHED_ACTIVE." AND (
            s.format_type_id = ".QST_TYPE_OBJECTIVE." OR (s.format_type_id = ".QST_TYPE_SHORT_ANSWER." AND s.question_id < 200000 AND s.publish_status = ".PUBLISHED_ACTIVE."))
            ORDER BY RAND() LIMIT 3)
    UNION
    (SELECT s.question_id
            FROM table_question s
            WHERE s.chapter_id = $chapter_id
            AND s.publish_status = ".PUBLISHED_ACTIVE." AND (
            s.format_type_id = ".QST_TYPE_SUBJECTIVE." OR (s.format_type_id = ".QST_TYPE_SHORT_ANSWER." AND s.question_id < 200000 AND s.publish_status = ".PUBLISHED_ACTIVE."))
            ORDER BY RAND() LIMIT 2)
Jacques Amar
  • 1,803
  • 1
  • 10
  • 12