2012-03-18 2 views
0

У меня есть следующие 4 выбирает:Mysql + Помощь Создание союзной Query

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 с истинным случайным возвращением?

ТНХ

ответ

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(); 
Смежные вопросы