C# 如何在联接查询中使用sql union

C# 如何在联接查询中使用sql union,c#,mysql,sql,sql-server,join,C#,Mysql,Sql,Sql Server,Join,这是我第一次来stackoverflow。我刚刚开始我的编程最终项目,但sql查询有问题。对不起,我的英语也不好 我有三张表,分别是zstatistics、zsuggestions和ZEntrycriteria。我有一个sql查询,它将结合zstatistics和zsuggestions,然后对结果进行排序以匹配最接近的结果 select M.* from zstatistics M JOIN ZEntrycriteria C ON M.coursecode=C.C

这是我第一次来stackoverflow。我刚刚开始我的编程最终项目,但sql查询有问题。对不起,我的英语也不好

我有三张表,分别是zstatistics、zsuggestions和ZEntrycriteria。我有一个sql查询,它将结合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))
但是我得到了一个语法错误,即如果语句包含UNION、INTERSECT或EXCEPT运算符,则异常详细信息:System.Data.SqlClient.SqlException:ORDER BY items必须出现在select列表中

谢谢您编辑了

如果您想使用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))

只要两个查询结果具有相同的列,您就可以像这样合并这两个结果。

您可以按照mark的建议在这两个查询之间使用UNION或UNION all。但是您需要知道,如果结果中的所有列都相同,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      

谢谢你的建议。这样做会给我一个语法错误异常详细信息:System.Data.SqlClient.SqlException:关键字“UNION”附近的语法不正确。@timton,这是因为在UNION之前不能有order By。删除该行,您可能还需要逐行返工上一个订单,以使其正常工作。@ZLK感谢您的评论。你能告诉我你所说的是什么意思吗?你可能还需要一行一行地修改上一个订单才能使它正常工作。如果可能的话,你能给我举个例子吗。这取决于你对结果的排序。将两个查询放入子查询是选项之一。@Mark..OMG您编辑的答案有效…您是一个天才。。非常感谢您的回复,我将在几个小时后尝试。所以我只在末尾按部分添加订单?它会对这两个表结果进行排序吗?强烈建议您在将查询嵌入应用程序之前使其工作。返回ManagementStudio并运行查询。它将为您提供更好的错误消息。