2011-07-20 87 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