联合返回具有限制 postgresql 的不同输出

Union returning different outputs with limit postgresql(联合返回具有限制 postgresql 的不同输出)
本文介绍了联合返回具有限制 postgresql 的不同输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须根据难度级别从我的问题集中提取 12 个问题.以下是我写的查询.

I have to fetch 12 questions from my question set based on the difficulty level. Following is the query I have written.

(SELECT q.question_text, q.option_a, q.option_b, q.option_c, q.option_d,
        q.correct_answer, q.image_link, q.question_type
        FROM questions_bank q
        JOIN sports_type st ON st.id = q.sports_type_id
        JOIN difficulty_level dl ON dl.id = q.difficulty_level_id
        WHERE st.game_type = LOWER('cricket') AND dl.value = 'E'
        ORDER BY random()
        LIMIT 7) 
UNION 
(SELECT q.question_text, q.option_a, q.option_b, q.option_c, q.option_d,
        q.correct_answer, q.image_link, q.question_type
        FROM questions_bank q
        JOIN sports_type st ON st.id = q.sports_type_id
        JOIN difficulty_level dl ON dl.id = q.difficulty_level_id
        WHERE st.game_type = LOWER('cricket') AND dl.value = 'M'
        ORDER BY random()
        LIMIT 4)
UNION 
(SELECT q.question_text, q.option_a, q.option_b, q.option_c, q.option_d, 
        q.correct_answer, q.image_link, q.question_type
        FROM questions_bank q
        JOIN sports_type st ON st.id = q.sports_type_id
        JOIN difficulty_level dl ON dl.id = q.difficulty_level_id
        WHERE st.game_type = LOWER('cricket') AND dl.value = 'H'
        ORDER BY random()
        LIMIT 1);

问题是,每当我运行这个查询时,每次它都会给我不同数量的结果,而不是静态的 12.有时我得到 12,有时 10,有时 15.我希望输出中有 12 行,而不是更少,不多.

The issue is that whenever I run this query, each time it gives me a different number of results instead of the static 12. Sometimes I get 12, sometimes 10, sometimes 15. I expect 12 rows in output, not less, not more.

这个查询有什么问题?

推荐答案

使用窗口函数可以简化为单个选择:

It can be reduced to a single select by using a window function:

select *
from (
    select
        row_number() over (partition by dl.value order by random()) as rn,
        dl.value,
        q.question_text, q.option_a, q.option_b, q.option_c, q.option_d,
        q.correct_answer, q.image_link, q.question_type
    from
        questions_bank q
        inner join
        sports_type st on st.id = q.sports_type_id
        inner join
        difficulty_level dl on dl.id = q.difficulty_level_id
    where st.game_type = lower('cricket') and dl.value in ('E','M','H')
) s
where 
    value = 'E' and rn <= 7 or
    value = 'M' and rn <= 4 or
    value = 'H' and rn = 1

这篇关于联合返回具有限制 postgresql 的不同输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)