检查具有特定ID的4个不同表中的列值以及失败的返回通过

时间:2016-02-16 06:15:52

标签: php mysql

Tabel_1:

Id    test_id  testname    passfail
1      1001       test1      pass
2      1001       test2      fail
3      1002       test11     pass
4      1002       test12     pass
5      1003       test21     fail
6      1003       test22     fail

Tabel_2:

Id    test_id  testname    passfail
1      1001       test1      pass
2      1001       test2      pass
3      1001       test3      pass
4      1002       test11     pass
6      1002       test13     fail
7      1002       test13     fail

Tabel_3:

Id    test_id  testname    passfail
4      1002       test11     pass
5      1002       test12     pass
6      1003       test31     fail
7      1003       test32     fail

Tabel_4:

Id    test_id  testname    passfail
1      1001       test1      pass
2      1001       test2      pass
3      1001       test3      pass
4      1003       test11     pass
5      1003       test12     pass

我想检查所有表(Tabel_1,Tabel_2,Tabel_3,Tabel_4)中的passfail列,并使用特定的test_id(如1001)或任何test_id。如果任何行失败,因为passfail列属于id,如1001那么查询将只返回一次失败或者在passfail列中找不到失败属于特定的test_id然后查询将只返回passfail colomn传递一次。

3 个答案:

答案 0 :(得分:1)

您可以使用CASE这样的表达式:

SELECT CASE 
          WHEN EXISTS (SELECT 1
                       FROM Table1
                       WHERE test_Id = 1001 AND passfail = 'fail'
                       UNION 
                       SELECT 1
                       FROM Table2
                       WHERE test_Id = 1001 AND passfail = 'fail'
                       UNION 
                       SELECT 1
                       FROM Table3
                       WHERE test_Id = 1001 AND passfail = 'fail'
                       UNION 
                       SELECT 1
                       FROM Table4
                       WHERE test_Id = 1001 AND passfail = 'fail')
           THEN 'fail'
           ELSE 'pass'
         END AS flag;

SELECT语句返回始终一行:如果四个表中任何一个中至少有一行有passfail = 'fail',则返回'fail',否则返回'pass'。

答案 1 :(得分:0)

您可以使用UNION DISTINCT

组合每个SELECT的结果
SELECT test_id FROM Table_1 WHERE passfail = 'fail'
UNION DISTINCT
SELECT test_id FROM Table_2 WHERE passfail = 'fail'
UNION DISTINCT
SELECT test_id FROM Table_3 WHERE passfail = 'fail'
UNION DISTINCT
SELECT test_id FROM Table_4 WHERE passfail = 'fail'

答案 2 :(得分:0)

您可以按UNION合并结果,并使用GROUP BY按某些字段进行分组:

List

结果:

Test

如果要将test_id添加到SELECT语句,则只能获得ID:

public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i])) // o in your case is a String while
                                          // elementData[i] is a Test
                                          // so String's equals returns false
                return i;
    }
    return -1;
}

结果:SELECT * FROM ( SELECT * FROM t1 WHERE passfail = 'pass' UNION SELECT * FROM t2 WHERE passfail = 'pass' UNION SELECT * FROM t3 WHERE passfail = 'pass' UNION SELECT * FROM t4 WHERE passfail = 'pass' ) as t GROUP BY test_id;

如果删除GROUP BY

,您还可以获取1 1001 test1 pass 4 1002 test12 pass 8 1003 test32 pass 3 10015 test11 pass 值的所有行
SELECT test_id FROM
  (
  SELECT * FROM t1 WHERE passfail = 'pass'
  UNION
  SELECT * FROM t2 WHERE passfail = 'pass'
  UNION
  SELECT * FROM t3 WHERE passfail = 'pass'
  UNION
  SELECT * FROM t4 WHERE passfail = 'pass'
) as t
GROUP BY test_id;

结果:

1001, 1002, 1003, 10015