跳过某些行的select语句

时间:2011-07-20 18:28:10

标签: php sql database tsql

我需要从两个表中选择数据。但是有两列,如果一行或两行读为NO,我想跳过它。

表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)

6 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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?

答案 2 :(得分:2)

为什么不说f = g和g ='YES'?

答案 3 :(得分: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

答案 4 :(得分:0)

怎么样:

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

答案 5 :(得分:-2)

您必须使用LEFT JOIN而不是INNER 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'