How to do a PostgreSQL query with where-in clause which contains multiple columns programmatically?

蓝咒 提交于 2020-01-14 02:28:08

问题


My query is like this:

select * from plat_customs_complex
where (code_t,code_s) 
in (('01013090','10'),('01029010','90'));

It runs well in psql console. My question is how to perform this query in client code.(via C# or Java)

And I already know the following code works well(C#):

string[] codeT = new string[]{"01013090","01029010"};    
connection.Query("SELECT * FROM plat_customs_complex WHERE code_t=ANY(@CodeT)",
new { CodeT = codeT });

回答1:


Finally, I found the unnest function can help.

Pure SQL is like that:

select * from plat_customs_complex
where (code_t,code_s) = ANY(select * from unnest(ARRAY['01013090','01029010'],ARRAY['10','90']))

Can convert it to C# code easily:

string[] codeTs = new string[]{"01013090","01029010"}; 
string[] codeSs = new string[]{"10", "90"};
connection.Query("select * from plat_customs_complex
where (code_t,code_s) = ANY(select * from unnest(@CodeTs, @CodeSs))", 
new {CodeTs=codeTs, CodeSs=codeSs});


来源:https://stackoverflow.com/questions/42943106/how-to-do-a-postgresql-query-with-where-in-clause-which-contains-multiple-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!