我希望能够设置一个字段来回答“对于此记录中的值,该值是否满足另一个表中的某些条件?”我想尝试一下带有exists
的case-when
,但是Teradata (我的数据库管理系统)不喜欢它。有什么建议吗?
select foo,
(case when exists (select x.foo
from somedb x
where x.bar > 0)
then '1' else '0' end) as MyFlag
from mydb
发布于 2010-07-28 22:49:47
对此可能有不止一种解决方案。有时,这两个表之间存在关系。然后,我创建一个连接并在WHERE子句中处理它。我不知道Teradata,但在Oracle中我也可以这样做。
SELECT foo
FROM mydb
WHERE (select count(*) from somedb where x.bar > 0) > 0
或者更像你的代码
select foo,
(case when (select count(*)
from somedb x
where x.bar > 0) > 0
then '1' else '0') as MyFlag
from mydb
我知道只在WHERE子句中使用EXISTS:“我只想要下面的SELECT给我一些东西的行”。只有当一个表和另一个表之间存在某种连接时,这才有意义。
select id,foo from mydb y
where exists (select x.id from somedb x where x.id = y.id)
https://stackoverflow.com/questions/3353987
复制相似问题