鉴于此表结构:
CREATE TABLE tags
(
id SERIAL NOT NULL PRIMARY KEY,
tagname TEXT NOT NULL UNIQUE,
authorid int NOT NULL,
created timestamp NOT NULL,
lastmodified timestamp NOT NULL,
constraint fk_authorid_tags foreign key(authorid) references users(id)
);
为什么以下查询失败并显示错误:
ERROR: operator does not exist: text = text[]
LINE 2: select * from tags where tagname in ('{"c#","c"}'::text[])
查询:
select * from tags where tagname in ('{"c#","c"}'::text[])
IN
必须包含文字列表,例如
tagname IN ('c#', 'c')
如果需要数组,则必须使用= ANY
:
tagname = ANY (ARRAY['c#', 'c'])
由于tagname IN (somearray)
被解释为查询“ tagname
等于1元素列表的任何元素(somearray)
”,因此出现错误。这意味着测试tagname
是否与somearray
,这是唯一的元素。由于没有=
运算符来比较text
和text[]
,因此失败。
相比之下, = ANY
说“对于右侧阵列的任何元素,左手操作数是否等于元素?”。所以它有效。