为什么union中的order by 会失效?

GoAndSeek 2007-12-26 02:46:54
(select suites.description as description,suites.id as id,users.ldap_uid as owner, clusters.name as cluster,

clusters.id as _clusters_id,suites.time_limit as _time_limit,suites.diff_folder as _diff_folder,
suites.recurrence as _recurrence, suites.ini_file as ini, suites.parent_group_id as

_parent_group_id,suite_groups.name as _parent_group_name,
concat_ws(' ',app_names.name, app_ver_major.ver_major, app_ver_minor.app_ver_minor) as application,
app_ver_major.id as _app_ver_major_id, app_ver_minor.id as _app_ver_minor_id, app_names.id as

_app_names_id, suites.baseline_folder as baseline
from suites join applications, app_names, app_ver_major, app_ver_minor, clusters, users,suite_groups on users.id =

suites.owner_id and applications.id = suites.application_id
and app_names.id = applications.app_name_id and app_ver_major.id = applications.app_ver_major_id and

app_ver_minor.id = applications.app_ver_minor_id and clusters.id = suites.cluster_id
and suites.parent_group_id = suite_groups.id
where suites.id =3194
order by suites.id desc)
union
(select "fill",suites.id as id,"fill", "fill", "fill" ,"fill","fill", "fill", "fill", "fill","fill",

"fill", "fill", "fill", "fill", suites.baseline_folder as baseline

from suites
where suites.suite_parent_id=3194 and suites.baseline_folder<>""
order by suites.id desc)

一个问题是在union中的order by suites.id desc失效了,输出的结果没有降序(若单独一个查询则成)
另外,不知道用"fill"填充以维持2个查询列匹配,这种做法是否能适用所有数据库,是否是标准的? 多谢了!
...全文
298 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shui_windows 2007-12-29
  • 打赏
  • 举报
回复
两个语句都取成相同的别名,在order by的时候用别名
ruihuahan 2007-12-29
  • 打赏
  • 举报
回复
order by 不能单独放在一个单独的select子句中,整个union只能有一个order by放在最后。
也就是说只能统一排序,不能单独排序然后再union。
GoAndSeek 2007-12-29
  • 打赏
  • 举报
回复
up
Well 2007-12-29
  • 打赏
  • 举报
回复
把order by 放在整个的后面。。。
heyixiang 2007-12-29
  • 打赏
  • 举报
回复
union包含了排序。

等同于你在子查询中排序后又重新排序一次。
GoAndSeek 2007-12-26
  • 打赏
  • 举报
回复
(select suites.description as description,suites.id as id,users.ldap_uid as owner, clusters.name as cluster,

clusters.id as _clusters_id,suites.time_limit as _time_limit,suites.diff_folder as _diff_folder,
suites.recurrence as _recurrence, suites.ini_file as ini, suites.parent_group_id as

_parent_group_id,suite_groups.name as _parent_group_name,
concat_ws(' ',app_names.name, app_ver_major.ver_major, app_ver_minor.app_ver_minor) as application,
app_ver_major.id as _app_ver_major_id, app_ver_minor.id as _app_ver_minor_id, app_names.id as

_app_names_id, suites.baseline_folder as baseline
from suites join applications, app_names, app_ver_major, app_ver_minor, clusters, users,suite_groups on users.id =

suites.owner_id and applications.id = suites.application_id
and app_names.id = applications.app_name_id and app_ver_major.id = applications.app_ver_major_id and

app_ver_minor.id = applications.app_ver_minor_id and clusters.id = suites.cluster_id
and suites.parent_group_id = suite_groups.id
where suites.id =3194)

union
(select "fill",suites.id as id,"fill", "fill", "fill" ,"fill","fill", "fill", "fill", "fill","fill",

"fill", "fill", "fill", "fill", suites.baseline_folder as baseline

from suites
where suites.suite_parent_id=3194 and suites.baseline_folder<>""
order by suites.id desc)

修改一下,问题同前

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧