이것은 "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
를 사용하려면
은 (편집)를
는 강하게 당신이 응용 프로그램에서 삽입하기 전에 작업 쿼리를 얻을 제안한다. Management Studio로 돌아가서 쿼리를 실행합니다. 더 나은 오류 메시지를 줄 것입니다. – dsz