ORDER BY NEWID() AND UNION 使用 MS SQL

ORDER BY NEWID() AND UNION using MS SQL

我正在尝试通过 MS 执行以下 SQL 查询。 SQL, objective 是 select 来自两个查询的随机记录并将它们合并在一起但它在 ORDER BY NEWID()

处生成错误

select 当 subject_id=1

时 2 个随机问题

联盟

select当subject_id=2

时随机3题
(SELECT TOP 2 REC_ID, REF_ID, SUBJECT_ID, QUESTIONS, ANSWERS 
FROM         dbo.questions WHERE     (SUBJECT_ID= 1) ORDER BY NEWID() ) 
UNION ALL    
(SELECT TOP 3 REC_ID, REF_ID, SUBJECT_ID, QUESTIONS, ANSWERS FROM 
dbo.questions WHERE     (SUBJECT_ID= 2) ORDER BY NEWID() )

试试这个。

SELECT * FROM
(
    SELECT TOP 2 REC_ID, REF_ID, SUBJECT_ID, QUESTIONS, ANSWERS 
    FROM dbo.questions 
    WHERE SUBJECT_ID= 1 
    ORDER BY NEWID()
) A
UNION ALL
SELECT * FROM    
(
    SELECT TOP 3 REC_ID, REF_ID, SUBJECT_ID, QUESTIONS, ANSWERS 
    FROM dbo.questions 
    WHERE SUBJECT_ID= 2
    ORDER BY NEWID()
) B