2011-07-20 86 views
1

我需要從兩個表中選擇數據。但是,有兩列,如果一行或兩行讀取否,我想跳過它。select語句跳過某些行

表1

a--b--c- 
1 r l 
2 t f 
3 d c 

表2

d--e--f--g- 
1 r NO NO 
2 r YES NO 
3 r YES YES 

QUERY:

SELECT 
    talbe1.a, 
    table1.b, 
    table1.c, 
    table2.d, 
    table2.e, 
    table2.f, 
    table2.g, 
    FROM table1 INNER JOIN 
    table2 on table1.b = table2.e 
    WHERE 'no' NOT IN (SELECT table2.f, table2.g FROM table2) 
+1

爲什麼不'其中f = 「無」 或G = 「不」'!? –

+1

@kerrek - 他需要'AND'而不是'OR'我認爲...... – JNK

+1

人們確實渴望提問。 2分鐘,10個答案+我沒有發佈的一個 –

回答

2
SELECT 
    talbe1.a, 
    table1.b, 
    table1.c, 
    table2.d, 
    table2.e, 
    table2.f, 
    table2.g, 
    FROM table1 INNER JOIN 
    table2 on table1.b = table2.e 
     //if any of the f or g are not eqaul to "NO" only then select the result 
     WHERE (table2.f != 'NO' AND table2.g != 'NO') 

does not return if 
f = no and g = no 
f = no and g = yes 
f = yes and g = no 

does return if 
f = yes and g = yes 
+1

刪除downvote,謝謝你的糾正! – JNK

+0

如何刪除幾個downvotes tooo ... – Vish

+0

需要編輯的問題/答案。如果你喜歡,我可以編輯它們。 – JNK

4

應該是這樣簡單:

SELECT 
    talbe1.a, 
    table1.b, 
    table1.c, 
    table2.d, 
    table2.e, 
    table2.f, 
    table2.g, 
    FROM table1 INNER JOIN 
     table2 on table1.b = table2.e 
    WHERE table2.f <> 'NO' AND table2.g <> 'NO' 
2

爲什麼不說f = g和g ='是'?

3

試試這個:

SELECT 
    table1.a, 
    table1.b, 
    table1.c, 
    table2.d, 
    table2.e, 
    table2.f, 
    table2.g, 
FROM table1 
LEFT JOIN table2 ON table1.b = table2.e 
WHERE table2.f <> 'NO' AND table2.g <> 'NO' 

而且不知道你的表的結構,但有你在table1.b = table2.e加入一個原因嗎?

0

如何:

WHERE (table2.f <> 'no' AND table2.g <> 'no') 
+0

應該是'AND',而不是'OR' ... – JNK

+0

不,它不應該。它應該是OR。如果聲明是肯定的,那麼它將是AND - WHERE table2.f ='yes'AND table2.g ='是' – Joshua

+0

不,它們都是。他只想要行,其中** BOTH **是'YES' – JNK

-2

,你必須使用LEFT JOININNER JOIN

SELECT 
    talbe1.a, 
    table1.b, 
    table1.c, 
    table2.d, 
    table2.e, 
    table2.f, 
    table2.g, 
    FROM table1 LEFT JOIN 
     table2 on table1.b = table2.e 
WHERE table2.f <> 'NO' AND table2.g <> 'NO' 
+0

你爲什麼認爲他需要一個'LEFT'而不是'INNER'? – JNK

+0

Bcz他從2個表格中獲取數據! –

+2

這意味着他應該如何使用'LEFT JOIN'? – JNK