(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个查询列匹配,这种做法是否能适用所有数据库,是否是标准的? 多谢了!

解决方案 »

  1.   

    (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)修改一下,问题同前
      

  2.   

    order by 不能单独放在一个单独的select子句中,整个union只能有一个order by放在最后。
    也就是说只能统一排序,不能单独排序然后再union。
      

  3.   

    两个语句都取成相同的别名,在order by的时候用别名
      

  4.   

    union包含了排序。等同于你在子查询中排序后又重新排序一次。
      

  5.   

    把order by 放在整个的后面