我希望合并两个具有不同ORDER BY
然而另一个查询的行为并不符合它的ORDER BY
我不知道是什么导致了这个bug,但它在我的本地文件中工作正常。但是当我在实时网站上启动时,第二个查询的行为效果不佳。
问题出现在sub-query table2
上它应该按starting_date descending
显示所有记录顺序,但查询不会那样排序......我不知道是什么造成了这种情况。
提前致谢
这是我的mysql查询。
SELECT * FROM (
SELECT
events.event_id,
events.event_name_en,
events.event_name_ch,
events.event_description_en,
events.event_description_ch,
events.ref,
events.start_date,
events.end_date,
events.venue_id,
events.organizers_id,
events.website,
events.payment_method,
events.visitors,
events.display,
events.booking_form,
events.map,
events.lmap,
events.insurance_offered,
events.masterhead_img,
events.icon_img,
events.event_token,
events.added_by as event_added_by,
events.added_date as event_added_date,
events.updated_by,
events.updated_date,
events.active,
events.allow_ond,
events.allow_ond_reg,
events.terms_p,
events.cancel_p,
events.no_show_p,
events.refund_p,
events.official_desc,
events.supporting_desc,
events.participating_desc,
events.official_title,
events.supporting_title,
events.participating_title,
events.order_number,
venue.venue_en,
venue.venue_ch,
venue.added_by as venue_added_by,
venue.added_date as venue_added_date,
venue.updated_by as venue_updated_by,
venue.updated_date as venue_updated_date,
countries.country_id,
countries.country_code,
countries.country_name
FROM
events
INNER JOIN venue
ON venue.venue_id=events.venue_id
INNER JOIN countries
ON countries.country_id=events.country_id
WHERE events.display = '1' AND events.order_number != ''
AND events.active = '1'
ORDER BY events.order_number ASC) as table1
UNION ALL
SELECT * FROM (SELECT
events.event_id,
events.event_name_en,
events.event_name_ch,
events.event_description_en,
events.event_description_ch,
events.ref,
events.start_date,
events.end_date,
events.venue_id,
events.organizers_id,
events.website,
events.payment_method,
events.visitors,
events.display,
events.booking_form,
events.map,
events.lmap,
events.insurance_offered,
events.masterhead_img,
events.icon_img,
events.event_token,
events.added_by as event_added_by,
events.added_date as event_added_date,
events.updated_by,
events.updated_date,
events.active,
events.allow_ond,
events.allow_ond_reg,
events.terms_p,
events.cancel_p,
events.no_show_p,
events.refund_p,
events.official_desc,
events.supporting_desc,
events.participating_desc,
events.official_title,
events.supporting_title,
events.participating_title,
events.order_number,
venue.venue_en,
venue.venue_ch,
venue.added_by as venue_added_by,
venue.added_date as venue_added_date,
venue.updated_by as venue_updated_by,
venue.updated_date as venue_updated_date,
countries.country_id,
countries.country_code,
countries.country_name
FROM
events
INNER JOIN venue
ON venue.venue_id=events.venue_id
INNER JOIN countries
ON countries.country_id=events.country_id
WHERE events.display = '1' AND events.order_number = ''
AND events.active = '1'
ORDER BY events.start_date DESC) as table2
结果:
前两排很好。 table1 sub-query
但是当你看第三行等等时...... starting_date desc
的顺序效果不好......第三行应该是world engineer blah.....
答案 0 :(得分:1)
来自MySQL手册页
http://dev.mysql.com/doc/refman/5.7/en/union.html
但是,对单个SELECT语句使用ORDER BY并不意味着行在最终结果中出现的顺序,因为UNION默认生成一组无序行。因此,在此上下文中使用ORDER BY通常与LIMIT结合使用,因此它用于确定要为SELECT检索的所选行的子集,即使它不一定影响这些行的顺序。最终的UNION结果。如果在SELECT中没有LIMIT出现ORDER BY,它会被优化掉,因为它无论如何都不会产生任何影响。
我认为你可以通过玩ORDER BY子句来做你想做的事: -
SELECT events.event_id,
events.event_name_en,
events.event_name_ch,
events.event_description_en,
events.event_description_ch,
events.ref,
events.start_date,
events.end_date,
events.venue_id,
events.organizers_id,
events.website,
events.payment_method,
events.visitors,
events.display,
events.booking_form,
events.map,
events.lmap,
events.insurance_offered,
events.masterhead_img,
events.icon_img,
events.event_token,
events.added_by as event_added_by,
events.added_date as event_added_date,
events.updated_by,
events.updated_date,
events.active,
events.allow_ond,
events.allow_ond_reg,
events.terms_p,
events.cancel_p,
events.no_show_p,
events.refund_p,
events.official_desc,
events.supporting_desc,
events.participating_desc,
events.official_title,
events.supporting_title,
events.participating_title,
events.order_number,
venue.venue_en,
venue.venue_ch,
venue.added_by as venue_added_by,
venue.added_date as venue_added_date,
venue.updated_by as venue_updated_by,
venue.updated_date as venue_updated_date,
countries.country_id,
countries.country_code,
countries.country_name
FROM events
INNER JOIN venue ON venue.venue_id = events.venue_id
INNER JOIN countries ON countries.country_id = events.country_id
WHERE events.display = '1'
AND events.active = '1'
ORDER BY IF(events.order_number = '', 1, 0),
IF(events.order_number = '', NULL, events.order_number) ASC,
IF(events.order_number = '', events.start_date, NULL) DESC
这将首先排序来自您的第一个子查询的所有记录,然后是来自第二个子查询的所有记录。在其中,它将通过order_number ASCending排序第一个,并通过start_date DESCending排序第二个。