2012-03-15 2 views
0

hiプログラマ!私はsqlのUnionでクエリを比較します

 As i need the result like 

      EMP_ID  TYPE 

      512  leave 
      612  weekend    

でもない

  EMP_ID  TYPE 


      512  leave 
      612  leave 
      612  weekend    
が好きで、これらの2つの選択クエリを比較する必要が

  select emp_id,type from 
      (
       select emp_id,'leave' as type 
       from tbl_emp_info 
       where emp_id like'5_2' or emp_id like '602' 
      ) as t1 
      union 
      select emp_id,type from 
      (
       select emp_id,'weekend'as type 
       from tbl_emp_off_info 
       where emp_id like'6_2' 
      ) t2 t1.emp_id!=t2.emp_id 

: 私は私のコード

ユニオンクエリの間で選択クエリを比較しているのは、このように書きます

助けてください

答えて

1

これはおそらくあなたのために働くでしょう(SQL Server 2005以降)。

;WITH t1 AS 
(
    SELECT emp_id, 'leave' AS [type] 
    FROM tbl_emp_info 
    WHERE emp_id LIKE '5_2' OR emp_id LIKE '602' 
) 
, t2 AS 
(
    SELECT emp_id, 'weekend' AS [type] 
    FROM tbl_emp_info 
    WHERE emp_id LIKE '6_2' 
) 
SELECT emp_id, [type] 
FROM t1 
WHERE emp_id NOT IN (SELECT emp_id FROM t2) 
UNION 
SELECT emp_id, [type] 
FROM t2 

SQL Server 2000のあなたはWHERE句の一つを複製する必要があります:あなたのソリューションのための

SELECT emp_id, 'leave' AS [type] 
FROM tbl_emp_info 
WHERE (emp_id LIKE '5_2' OR emp_id LIKE '602') 
AND NOT (emp_id LIKE '6_2') 
UNION 
SELECT emp_id, 'weekend' AS [type] 
FROM tbl_emp_info 
WHERE (emp_id LIKE '6_2') 
+0

おかげで、それは次のようにエラーを与える:1行目:付近に正しくない構文「;」。実際には、私はSQL Server 2000を使用して、それはそのバージョンのために発生しますか? –

+0

SQL Server 2005でこのコードをチェックしても動作しますが、SQL Server 2000では動作しませんが、SQL Server 2000でこの動作を実行するにはどうしますか? –

+0

SQL Server 2000用のソリューションを追加しました。 –

関連する問題