有没有办法在values
子查询中指定列的数据类型?像(values (...)) as tmp_name (colname::type)
?
假设我有一个包含uuid
列的表格:
/* setup */
create table foo (id uuid);
insert into foo values
('aaaabbbbccccddddeeeeffff00001111'),
('aaaabbbbccccddddeeeeffff00002222'),
('aaaabbbbccccddddeeeeffff00003333');
如果我尝试join
使用values
子查询对其进行反对,postgres会假定该列为text
类型并抛出类型错误(即使相同的字符串有效)直接插入uuid
列):
select foo.id from foo
inner join (values
('aaaabbbbccccddddeeeeffff00001111'),
('aaaabbbbccccddddeeeeffff00000000'))
as tmp (id) on foo.id = tmp.id;
ERROR: operator does not exist: uuid = text
LINE 5: as tmp (id) on foo.id = tmp.id;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
明确投出每个值:
如果我将显式强制转换添加到值本身,它会起作用,但是不必抛出每个值并且只是指定列的数据类型是很好的:
select foo.id from foo
inner join (values
('aaaabbbbccccddddeeeeffff00001111'::uuid),
('aaaabbbbccccddddeeeeffff00000000'::uuid))
as tmp (id) on foo.id = tmp.id;
答案 0 :(得分:1)
您可以在连接谓词上投射一次:
select foo.id from foo
inner join (values
('aaaabbbbccccddddeeeeffff00001111'),
('aaaabbbbccccddddeeeeffff00000000'))
as tmp (id) on foo.id = tmp.id::uuid;