栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将列表传递给where子句

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将列表传递给where子句

试试这个:

create table x(d timestamp);insert into x values('jan 2, 2012'),('february 4, 2012 12:00'),('jan 4, 2012 12:00'),('march 1, 2012'),('may 3, 2012');

询问:

with input as(  select   '2012-1-2, 2012-01-04 12:00, 2012-02-04 12:00, 2012-03-04 12:00'::text  as d_input),converted_to_array as(  select ('{' || d_input || '}')::timestamp[] as d_array  from input )select dfrom x cross join converted_to_arraywhere d = any(d_array)

输出:

DJanuary, 02 2012 00:00:00-0800February, 04 2012 12:00:00-0800January, 04 2012 12:00:00-0800

实时测试:http://www.sqlfiddle.com/#!1 /
43d48 / 26


您还可以使用IN,只是将数组嵌套到行:

with input as(  select   '2012-1-2, 2012-01-04 12:00, 2012-02-04 12:00, 2012-03-04 12:00'::text  as d_input),converted_to_array as(  select ('{' || d_input || '}')::timestamp[] as d_array  from input )select dfrom x cross join converted_to_arraywhere d in (select unnest(d_array))

实时测试:http://www.sqlfiddle.com/#!1
/
43d48 /
29


您也可以将它们全部放在一行中:

select dfrom x where d in (select unnest( ('{' || '2012-1-2, 2012-01-04 12:00, 2012-02-04 12:00, 2012-03-04 12:00'::text || '}')::timestamp[] ))

但我犹豫这样做,因为这会在stackoverflow上引起水平滚动条:-)

实时测试:http://www.sqlfiddle.com/#!1 /
43d48 / 31



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/456262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号