我需要2个查询的并集,但结果必须按特定顺序排列:
我写了这个查询:
SELECT * FROM (
(SELECT c.category_id as media_id,
null as media_users_id,
c2l.category_name as media_title,
null as media_duration,
null as media_upload_timestamp,
null as media_key,
null as media_conversion_info,
null as media_type,
null as media_views,
null as media_embed_views,
null as media_comments_count,
null as media_accept_ratings,
null as media_accept_comments,
NOW() as media_upload_date,
null as media_ratings_sum,
null as media_ratings_count,
null as categories2media_category_id,
null as categories2media_media_id,
1 as type
FROM ost_categories c
LEFT JOIN ost_categories2language c2l
ON c2l.category_id = c.category_id
WHERE c2l.category_language = 'russian' AND c.category_parent_id = '60' AND c.is_group = '1'
ORDER BY media_title)
UNION
(SELECT
m.media_id,
m.media_users_id,
m.media_title,
m.media_duration,
UNIX_TIMESTAMP(m.media_upload_date) media_upload_timestamp,
m.media_key,
m.media_conversion_info,
m.media_type,
m.media_views,
m.media_embed_views,
m.media_comments_count,
m.media_accept_ratings,
m.media_accept_comments,
m.media_upload_date,
m.media_ratings_sum,
m.media_ratings_count,
c2m.categories2media_category_id,
c2m.categories2media_media_id,
0 as type
FROM ost_media m,
ost_categories2media c2m
WHERE c2m.categories2media_category_id = '60'
AND c2m.categories2media_media_id = m.media_id
AND m.media_type = 'video'
AND (m.media_access='public' OR (m.media_access='private' AND m.media_users_id=2))
AND m.media_conversion_status = 'success'
AND m.media_notified_status = 'legal' ORDER BY m.media_upload_date DESC)
) t LIMIT 0,28
但是排序错了。我怎么解决这个问题?
答案 0 :(得分:0)
UNION
并不关心如何对查询结果进行排序,您必须指定UNION
以外的订单。
你必须做这样的事情:
SELECT * FROM (
(SELECT c.category_id as media_id,
null as media_users_id,
c2l.category_name as media_title,
null as media_duration,
null as media_upload_timestamp,
null as media_key,
null as media_conversion_info,
null as media_type,
null as media_views,
null as media_embed_views,
null as media_comments_count,
null as media_accept_ratings,
null as media_accept_comments,
NOW() as media_upload_date,
null as media_ratings_sum,
null as media_ratings_count,
null as categories2media_category_id,
null as categories2media_media_id,
1 as type
c2l.category_name AS ordername
FROM ost_categories c
LEFT JOIN ost_categories2language c2l
ON c2l.category_id = c.category_id
WHERE c2l.category_language = 'russian' AND c.category_parent_id = '60' AND c.is_group = '1')
UNION
(SELECT
m.media_id,
m.media_users_id,
m.media_title,
m.media_duration,
UNIX_TIMESTAMP(m.media_upload_date) media_upload_timestamp,
m.media_key,
m.media_conversion_info,
m.media_type,
m.media_views,
m.media_embed_views,
m.media_comments_count,
m.media_accept_ratings,
m.media_accept_comments,
m.media_upload_date,
m.media_ratings_sum,
m.media_ratings_count,
c2m.categories2media_category_id,
c2m.categories2media_media_id,
0 as type
"" AS ordername
FROM ost_media m,
ost_categories2media c2m
WHERE c2m.categories2media_category_id = '60'
AND c2m.categories2media_media_id = m.media_id
AND m.media_type = 'video'
AND (m.media_access='public' OR (m.media_access='private' AND m.media_users_id=2))
AND m.media_conversion_status = 'success'
AND m.media_notified_status = 'legal')
) t
ORDER BY t.type DESC, t.ordername ASC, t.media_upload_date DESC
LIMIT 0,28
注意:我使用type
根据行是来自查询1还是查询2进行排序。我引入了ordername
列,并将其设置为“”以用于查询2,以便它没有对这些行的顺序产生影响。
答案 1 :(得分:0)
对内部选择进行排序不会影响生成的UNION查询 您只能从一个接一个的选择中选择行。 例如,
(select 1 sort_by, .....)
union
(select 2, .....) order by sort_by.
同样在这种情况下,您可以通过sort_by,media_title对组内的行进行排序,但是这一行将影响每个结果集中的顺序。 查看http://dev.mysql.com/doc/refman/5.1/en/union.html
中的更多信息