Tabel_1:
Id test_id testname passfail
1 1001 test1 pass
2 1001 test2 fail
3 1002 test11 pass
4 1002 test12 pass
5 1002 test13 fail
Tabel_2:
Id test_id testname passfail
1 1001 test1 pass
2 1001 test2 pass
3 1001 test3 pass
4 1002 test11 pass
5 1002 test12 pass
6 1002 test13 fail
7 1002 test13 fail
我想使用特定test_id(如 1001(检查表(Tabel_1 和 Tabel_2( 中的通过失败列。 如果任何行失败且特定 ID 为 10001,则查询将仅返回一次失败,或者如果任何行在列中没有失败 passfail,则返回值将为 pass。
你可以这样做:
SELECT test_id,min(passfail) as passfail FROM(
SELECT * from table_1
UNION ALL
SELECT * FROM table_2) as unioned
WHERE test_id = 1001
GROUP BY test_id
您不需要联接此表,并且所有表通常都比联接具有更好的性能。您可以为每个选择 min(passfail(,因为 fail 较小,因此如果有 fail 值,则会返回该值,如果没有,则将返回 pass。