设计考试系统的时候
遇到要按比列出题的问题。如何设计Sql查询。效率最高?灵活性更强。
比如查询判读题4题。选择题6题。在一个表中select top 4 * from exam where type_t='选择题' UNION select top 6 * from exam where type_t='判断题'可以不?
如果要满足某一类中的判断题两个条件。怎么写。
遇到要按比列出题的问题。如何设计Sql查询。效率最高?灵活性更强。
比如查询判读题4题。选择题6题。在一个表中select top 4 * from exam where type_t='选择题' UNION select top 6 * from exam where type_t='判断题'可以不?
如果要满足某一类中的判断题两个条件。怎么写。
================
这样完全可以!!如果要满足某一类中的判断题两个条件
=======================
where后面继续加条件!
如
.....where type_t='选择题' and xxx='yyyy' UNION .....
比如查询判读题4题。选择题6题。在一个表中 select top 4 * from exam where type_t='选择题' UNION select top 6 * from exam where type_t='判断题'
select top 4 * from exam where type_t="选择题"
UNION select top 6 * from exam where type_t='判断题' and newid()
那么应该select top 4 * from exam where type_t='选择题' order by newid()用了order by就不能union
可能要分两次取
另外,楼取如果同时取两种不同的题目,用union all 比较好,速度理论上比union快
因为同种题目不存在重复,不同的题型已经加条件限制,也不可能重复select top 4 * from exam where type_t='选择题'
UNION all
select top 6 * from exam where type_t='判断题'
——————————————————————用了order by就不能union
可能要分两次取请详细解释一下。可以不?