これは私が "stackoverflow"で初めてのことです。私はちょうど私のプログラミングの最終プロジェクトを開始し、SQLクエリに問題がある。 (私の悪い英語にも申し訳ありません)結合されたクエリでSQL UNIONを使用する方法
私はzstatistics、zsuggestions、ZEntrycriteriaという3つのテーブルを持っています。私はzstatistics、zsuggestionsの両方を "UNION"し、最も近い結果に一致するように結果を並べ替える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:if文ORDER BY項目は、選択リストに表示されなければなりませんUNION、INTERSECTまたはEXCEPT演算子が含まれています。あなたがUNION
を使用したい場合は
はちょうどあなたのクエリの間に入れて、(編集済み)
強くおアプリケーションでそれを埋め込む前に、作業のクエリを取得示唆しています。 Management Studioに戻り、クエリを実行します。より良いエラーメッセージが表示されます。 – dsz