如何将数组转换为类型?

How to cast arrays to a type?

我希望此查询 return 所有未被查询 #2 return 编辑的 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 行的 select 附近收到以下错误:

operator does not exist: integer = integer[]   
You might need to add explicit type casts.

我尝试将数组转换为整数并得到:

cannot cast type integer[] to integer

我还尝试将 my_table.id 和数组都转换为 varchar。这消除了错误,但 returned 行数比我预期的少!

第一个问题:为什么我不能将 integer[] 转换为 integer?数组的默认数据类型是什么?有没有比将 my_table.id 和数组都转换为 varchar 更简洁的方法来消除 "operator does not exist" 错误?

然后跟进:现在我想知道为什么我得到的行数太少。它看起来像我写的方式 return 我想要的吗?即所有未被查询 #2?

return 编辑的 ID

另一个约束 - 我需要用一条语句完成所有事情。

您需要删除 select arrayIN 运算符查找的是单列查询结果,不是数组。

删除错位的 array constructor 后,您将得到一个有效的查询。
IN and 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;

更多信息:

  • Select rows which are not present in other table