2017-03-17 16 views
1

これは私が "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を使用したい場合は

はちょうどあなたのクエリの間に入れて、(編集済み)

+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'を持つことができないからです。その行を削除してください(また、最後の 'order by'行を再作成する必要があるかもしれません)。 – ZLK

+0

@ ZLKコメントありがとうございました。あなたはどういう意味なのか教えてください。 "また、最後の注文をラインでも作業してもらう必要があるかもしれません"。可能であれば、私に例を教えてください。 – timton

0

マークで示唆されているように、これら2つのクエリの間で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 

それとも、重複行を返すように照会したい場合は、直接ALL 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

はい注文を1回だけ使用する必要があります – user2164964

関連する問題