Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query IN Clause with parameter

Here is my query,

<Request method="GET">
   <Query>
     select user_id from user
     where user_type in ($userTypes)
   </Query>
</Request>

How do I sent multiple values for this parameter $userType in metamug resource file. I'm using a GET request. And my database is postgres.

$userTypes is list of ids. I can pass it as comma separated string "1501,1502,1503" in the request.

Thanks.

like image 278
Sorter Avatar asked Feb 11 '26 17:02

Sorter


2 Answers

You could use:

select user_id from user
where user_type in (SELECT unnest(string_to_array($userTypes, ',')));

DbFiddle Demo

like image 198
Lukasz Szozda Avatar answered Feb 13 '26 17:02

Lukasz Szozda


you can also write it as

<Request method="GET">
   <Query>
     select user_id from user 
     where user_type =any(string_to_array($userTypes,',')::int[]);
   </Query>
</Request>

I've typecast it as Integer array you can use any other datatype as per your need.

like image 25
Kainix Avatar answered Feb 13 '26 17:02

Kainix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!