2012-09-05 81 views
0

我有兩個表如下。存在關鍵字不工作

表VAL1

e_id name val 

1  A1  Abc   
2  A2  Abd   
3  A3  AbE   
4  A4  AEd 
5  A5  AEd 
6  A6  Bdc 

表val2的

e_id e_desc t_id 

1  desc1 1 
2  desc1 1 
1  desc1 2 
3  desc1 1 
5  desc1 1 
2  desc1 2 
4  desc1 2 
5  desc1 2 
2  desc1 3 
4  desc1 1 
6  desc1 1 
3  desc1 2 
6  desc1 2 
3  desc1 2 
5  desc1 3 

欲通過E_ID地圖E_ID上表VAL2取從表VAL1只值,其中表VAL2 T_ID = 1

我我正在使用此查詢,但它正在提取所有數據。我該如何解決這個問題。這裏是我使用的SQL代碼如下所示

SELECT 
    a.*, 
    b.e_desc 
FROM 
    val1 AS a, val2 AS b 
WHERE 
    b.e_id = a.e_id 
AND EXISTS (SELECT 
       c.e_id 
      FROM val2 AS c 
      WHERE 
       c.e_id = a.e_id 
      AND c.t_id='1' 
      ) 

回答

1

它可以通過JOIN解決兩個表與兩個條件。試試這個,

SELECT a.*, b.e_desc 
FROM tableA a 
     INNER JOIN tableB b 
      ON a.e_id = b.e_id AND 
       a.e_id = b.t_id 
WHERE b.t_id = 1 
2

如何只

SELECT a.*, 
     b.e_desc 
FROM val1 AS a, 
     val2 AS b 
WHERE b.e_id = a.e_id 
AND  b.t_id='1' 

甚至

SELECT a.*, 
     b.e_desc 
FROM val1 AS a INNER JOIN 
     val2 AS b ON b.e_id = a.e_id 
WHERE b.t_id='1' 
+0

OK,讓我試着給你很快反饋。 – Jhilom