Mysql +帮助创建一个UNION查询

时间:2012-03-18 03:43:01

标签: mysql

我有以下4个选择:

 select english,type from table where type='brief' ORDER BY RAND() LIMIT 5;
 select english,type from table where type='small' ORDER BY RAND() LIMIT 5;
 select english,type from table where type='medium' ORDER BY RAND() LIMIT 5;
 select english,type from table where type='large';

而不是进行多次查询,我宁愿将其作为一个UNION运行 - 也看到所有输出的格式相同。

我尝试了以下但是RAND()似乎不起作用。每种类型大约有10个选项,但我一直看到相同的5而不是随机返回:

 select english,type from table where type='brief' LIMIT 5
 UNION
 select english,type from table where type='small' LIMIT 5
 UNION
 select english,type from table where type='medium' LIMIT 5
 UNION
 select english,type from table where type='large' ORDER BY type,RAND();

有关如何以真正的随机回报运行此UNION的任何建议吗?

THX

1 个答案:

答案 0 :(得分:1)

如果您想通过整个联盟订购,您需要将联合包含在另一个选项中,如下所示:

SELECT (
select english,type from table where type='brief' LIMIT 5
UNION
select english,type from table where type='small' LIMIT 5
UNION
select english,type from table where type='medium' LIMIT 5
UNION
select english,type from table where type='large'
)
ORDER BY type, RAND();
相关问题