2017-03-17 1 views
1

이것은 "stackoverflow"에서 처음입니다. 나는 방금 내 프로그래밍 최종 프로젝트를 시작하고 SQL 쿼리에 문제가있다. (나쁜 영어로도 죄송합니다)결합 된 쿼리와 함께 sql union을 사용하는 방법

나는 zstatistics, zsuggestions 및 ZEntrycriteria라는 세 개의 테이블이 있습니다. "결합"zstatistics, zsuggestions 및 가장 가까운 결과를 일치하도록 결과 순서를 SQL 쿼리가 있습니다.

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([email protected],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([email protected],2) + power(M.Geography - @geography,2) + power(M.Art - @Art,2)) 

다음 시도하지만 난 "예외 정보를 말하는 구문 오류가 점점 오전 : System.Data.SqlClient.SqlException : ORDER BY 항목이 문 경우 선택 목록에 표시해야합니다 UNION, INTERSECT 또는 EXCEPT 연산자가 포함되어 있습니다. " 당신은 당신이 쿼리 사이에 그냥 넣어 UNION를 사용하려면

은 (편집)를

+0

는 강하게 당신이 응용 프로그램에서 삽입하기 전에 작업 쿼리를 얻을 제안한다. Management Studio로 돌아가서 쿼리를 실행합니다. 더 나은 오류 메시지를 줄 것입니다. – dsz

답변

1

감사드립니다. 최종 쿼리는 한 두 쿼리 결과는 동일한 열을 가지고

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([email protected],2) + power(t.Geography - @geography,2) + power(t.Art - @Art,2)) 

처럼이 같은 결과의 다음 수 UNION 둘 것입니다.

+0

제안 해 주셔서 감사합니다. 이렇게하면 구문 오류가 발생합니다. "예외 정보 : System.Data.SqlClient.SqlException : 키워드 'UNION'근처에 구문이 잘못되었습니다." – timton

+0

@timton, 왜냐하면 당신은'union' 전에'order by '를 가질 수 없기 때문입니다. 해당 줄을 제거하십시오 (마지막 줄 순서대로 다시 작업해야 할 수도 있습니다). – ZLK

+0

@ ZLK 의견을 주셔서 감사합니다. 당신은 나에게 무엇을 의미하는지 말해 주실 수 있겠습니까? "그리고 당신은 작업을하기 위해서 마지막 주문을 라인별로 재 작업해야 할 수도 있습니다." 가능한 경우 나에게 예제를 제공 할 수 있습니다. – timton

0

마크로 제안 된대로이 두 쿼리 사이에 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을 사용할 수 있습니다

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  
+0

답장을 보내 주셔서 감사 드리며 몇 시간 안에 해보겠습니다. 따라서 "주문"부분을 끝에 추가합니다.두 테이블 결과를 모두 주문할 예정입니까? – timton

+0

예 한 번만 주문을 사용해야합니다. – user2164964

관련 문제