将 WHERE 子句添加到查询
Add WHERE clause to a query
我有以下查询:
select count(case when test_nameb is null then 1 end)
from ( select a.test_name as test_namea, b.test_name as test_nameb
from list_52 a
left join motorcontroltests b
on a.test_name=b.test_name) x
通过这个查询,我可以计算 test_name 列上两个 table 之间的不匹配。
但我想在 table motorcontroltests 的查询中添加一个 WHERE 子句。而且我不知道把这个条款放在哪里。我试过了,但总是出错。
提前致谢。
只需在加入后使用 where
子句,然后 on
select count(case when test_nameb is null then 1 end)
from ( select a.test_name as test_namea, b.test_name as test_nameb
from list_52 a
left join motorcontroltests b
on a.test_name=b.test_name
where b.test_name is not null --assuming you want to null check---put your condition
) x
但我认为你可以使用 join
select sum(case when b.test_name is null then 1 else 0 end)
from list_52 a left join
motorcontroltests b
on a.test_name=b.test_name
我不知道如何处理 where
子句,但你可以避免 subquery
:
select count(*)
from list_52 a
where not exists (select 1
from motorcontroltests b
where a.test_name = b.test_name and b.version = '2.0'
);
可以直接表达为 via JOIN
:
select sum(case when b.test_name is null then 1 else 0 end)
from list_52 a left join
motorcontroltests b
on a.test_name = b.test_name and b.version = '2.0';
尝试将 b.version
与 JOIN
放在一起:
SELECT SUM(CASE WHEN b.test_name IS NULL THEN 1 ELSE 0 END) Count
FROM list_52 a left join motorcontroltests b on a.test_name=b.test_name
and b.version='2.0'
尽管您可以将查询写成:
select count(case when test_nameb is null then 1 end)
from (select l.test_name as test_namea, mct.test_name as test_nameb
from list_52 l left join
motorcontroltests mct
on l.test_name = mct.test_name and mct.version = '2.0'
) x;
然而,这将更简单地写为:
select count(*)
from list_52 l left join
motorcontroltests mct
on l.test_name = mct.test_name and mct.version = '2.0'
where mct.test_name is null;
也许更清楚:
select count(*)
from list_52 l
where not exists (select 1
from motorcontroltests mct
where l.test_name = mct.test_name and mct.version = '2.0'
);
请注意 table 别名是 table 名称的缩写,而不是任意字母。这使得理解和修改查询变得更加容易。
我有以下查询:
select count(case when test_nameb is null then 1 end)
from ( select a.test_name as test_namea, b.test_name as test_nameb
from list_52 a
left join motorcontroltests b
on a.test_name=b.test_name) x
通过这个查询,我可以计算 test_name 列上两个 table 之间的不匹配。 但我想在 table motorcontroltests 的查询中添加一个 WHERE 子句。而且我不知道把这个条款放在哪里。我试过了,但总是出错。
提前致谢。
只需在加入后使用 where
子句,然后 on
select count(case when test_nameb is null then 1 end)
from ( select a.test_name as test_namea, b.test_name as test_nameb
from list_52 a
left join motorcontroltests b
on a.test_name=b.test_name
where b.test_name is not null --assuming you want to null check---put your condition
) x
但我认为你可以使用 join
select sum(case when b.test_name is null then 1 else 0 end)
from list_52 a left join
motorcontroltests b
on a.test_name=b.test_name
我不知道如何处理 where
子句,但你可以避免 subquery
:
select count(*)
from list_52 a
where not exists (select 1
from motorcontroltests b
where a.test_name = b.test_name and b.version = '2.0'
);
可以直接表达为 via JOIN
:
select sum(case when b.test_name is null then 1 else 0 end)
from list_52 a left join
motorcontroltests b
on a.test_name = b.test_name and b.version = '2.0';
尝试将 b.version
与 JOIN
放在一起:
SELECT SUM(CASE WHEN b.test_name IS NULL THEN 1 ELSE 0 END) Count
FROM list_52 a left join motorcontroltests b on a.test_name=b.test_name
and b.version='2.0'
尽管您可以将查询写成:
select count(case when test_nameb is null then 1 end)
from (select l.test_name as test_namea, mct.test_name as test_nameb
from list_52 l left join
motorcontroltests mct
on l.test_name = mct.test_name and mct.version = '2.0'
) x;
然而,这将更简单地写为:
select count(*)
from list_52 l left join
motorcontroltests mct
on l.test_name = mct.test_name and mct.version = '2.0'
where mct.test_name is null;
也许更清楚:
select count(*)
from list_52 l
where not exists (select 1
from motorcontroltests mct
where l.test_name = mct.test_name and mct.version = '2.0'
);
请注意 table 别名是 table 名称的缩写,而不是任意字母。这使得理解和修改查询变得更加容易。