我希望此查询返回查询 #2 未返回的所有 ID 和相关电子邮件:
select my_table.id, my_table.email
from my_table join another_table on my_table.id=another_table.mytable_id
where my_table.id not in (select array (select my_table.id
from my_table join yetanother_table
on my_table.id = yetanother_table.mytable_id
where yetanother_table.time1 between '2015-01-26T08:00:00.000Z'
and '2015-02-02T07:59:59.999Z'
group by my_table.id))
当我运行它时,在第 3 行的选择附近出现以下错误:
operator does not exist: integer = integer[] You might need to add explicit type casts.
我尝试将数组转换为整数并得到:
cannot cast type integer[] to integer
我还尝试将 my_table.id
和数组转换为 varchar
。这消除了错误,但返回的行数比我预期的要少!
第一个问题:为什么我不能将 integer[]
转换为 integer
?数组的默认数据类型是什么?有没有比将 my_table.id 和数组都转换为 varchar
更简洁的方法来消除“运算符不存在”错误?
然后跟进:现在我想知道为什么我得到的行数太少。看起来我写的方式会返回我想要的吗?即查询 #2 未返回的所有 ID?
另一个约束 - 我需要用一个语句来做所有事情。
最佳答案
删除错位的 array constructor 后,你会得到一个有效的查询。
IN
和 NOT IN
构造需要向右生成匹配类型的普通子查询 - 不是数组。
不过,查询仍然是扭曲且低效的。改用:
select m.id, m.email
from my_table m
join another_table a on a.mytable_id = m.id
left join yetanother_table y on y.mytable_id = m.id
and y.time1 >= '2015-01-26 08:00'
and y.time1 < '2015-02-02 08:00'
where y.mytable_id IS NULL;
更多信息:
关于sql - 如何将数组转换为类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28306020/