如何使用加入查询的sql union

时间:2017-03-17 03:43:11

标签: c# mysql sql sql-server join

这是我第一次进入" stackoverflow"。我刚刚开始我的编程最终项目并且遇到了sql查询的问题。 (抱歉我的英语不好)

我有三个名为zstatistics,zsuggestions和ZEntrycriteria的表。我有一个sql查询,将" UNION" zstatistics,zsuggestions然后命令结果匹配最接近的结果。

    select M.* 
          from zstatistics M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code
          where C.Maths <= @maths
            AND C.Science <= @science
            AND C.English <= @english
            And C.Ict <= @ict
            And C.History <= @history
            And C.Geography <= @geography
            And C.Art <= @Art

UNION 

        select M.* 
         from zsuggestions M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code 
         where C.Maths <= @maths 
           AND C.Science <= @science 
           AND C.English <= @english 
           And C.Ict <= @ict And C.History <= @history 
           And C.Geography <= @geography 
           And C.Art <= @Art  
ORDER BY 

      sqrt( power(M.Maths - @maths, 2) + power(M.Science - @science,2) + power(M.English - @english,2) + power(M.Ict - @ict,2) + power(M.History-@history,2) + power(M.Geography - @geography,2) + power(M.Art - @Art,2))

我也试过了

select * from
(
select M.* 
          from zstatistics M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code
          where C.Maths <= @maths
            AND C.Science <= @science
            AND C.English <= @english
            And C.Ict <= @ict
            And C.History <= @history
            And C.Geography <= @geography
            And C.Art <= @Art

UNION 

        select M.* 
         from zsuggestions M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code 
         where C.Maths <= @maths 
           AND C.Science <= @science 
           AND C.English <= @english 
           And C.Ict <= @ict And C.History <= @history 
           And C.Geography <= @geography 
           And C.Art <= @Art  
)
ORDER BY 

      sqrt( power(M.Maths - @maths, 2) + power(M.Science - @science,2) + power(M.English - @english,2) + power(M.Ict - @ict,2) + power(M.History-@history,2) + power(M.Geography - @geography,2) + power(M.Art - @Art,2))

但我收到一个语法错误,说&#34;异常详细信息:System.Data.SqlClient.SqlException:如果语句包含UNION,INTERSECT或EXCEPT运算符,则ORDER BY项必须出现在选择列表中。&#34 ;

谢谢(编辑)

2 个答案:

答案 0 :(得分:1)

如果您想使用UNION,请将其放在查询之间。最后的查询就像

select * from
(
select M.* 
  from zstatistics M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code
  where C.Maths <= @maths
    AND C.Science <= @science
    AND C.English <= @english
    And C.Ict <= @ict
    And C.History <= @history
    And C.Geography <= @geography
    And C.Art <= @Art

UNION

select M.* 
from zsuggestions M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code 
where C.Maths <= @maths
  AND C.Science <= @science
  AND C.English <= @english
  And C.Ict <= @ict
  And C.History <= @history
  And C.Geography <= @geography
  And C.Art <= @Art
  ) t

 ORDER BY sqrt( power(t.Maths - @maths, 2) + power(t.Science - @science,2) + power(t.English - @english,2) + power(t.Ict - @ict,2) + power(t.History-@history,2) + power(t.Geography - @geography,2) + power(t.Art - @Art,2))

只要两个查询结果都有相同的列,那么您可以UNION这两个结果。

答案 1 :(得分:0)

您可以在这两个查询之间使用union或union all,如mark所示。但是你需要知道UNION删除重复记录(结果中的所有列都相同),UNION ALL不会。

 select M.* 

  from zstatistics M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code
  where C.Maths <= @maths
   AND C.Science <= @science
    AND C.English <= @english
    And C.Ict <= @ict
    And C.History <= @history
    And C.Geography <= @geography
    And C.Art <= @Art

UNION

select M.* 
from zsuggestions M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code 
where C.Maths <= @maths
  AND C.Science <= @science
  AND C.English <= @english
  And C.Ict <= @ict
  And C.History <= @history
  And C.Geography <= @geography
  And C.Art <= @Art

如果您希望查询返回重复的行,您可以直接使用UNION ALL:

 select M.* 
  from zstatistics M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code
  where C.Maths <= @maths
   AND C.Science <= @science
    AND C.English <= @english
    And C.Ict <= @ict
    And C.History <= @history
    And C.Geography <= @geography
    And C.Art <= @Art

UNION ALL

select M.* 
from zsuggestions M JOIN ZEntrycriteria C ON M.coursecode=C.Course_code 
where C.Maths <= @maths
  AND C.Science <= @science
  AND C.English <= @english
  And C.Ict <= @ict
  And C.History <= @history
  And C.Geography <= @geography
  And C.Art <= @Art