# SELECT...WHERE...IN - using array values

**URL:** <https://forum.servoy.com/t/select-where-in-using-array-values/3039>\
**Category:** Classic Servoy\
**Created:** [November 10, 2004, 12:32pm UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039 "2004-11-10T12:32:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [November 10, 2004, 12:32pm UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/1 "2004-11-10T12:32:51Z")

</div>

I am trying to build an sql query that works as a reduce foundset option (the query spans multiple tables and I cannot use the Servoy reduce find option). As far as I can work out, I need a series of values in inverted commas as match values when I run the new query eg. ```  
var query = 'SELECT contactsid FROM contacts WHERE contactsid IN (‘1’, ‘6’, ‘8’)

```but

```

var set = databaseManager.getFoundSetDataProviderAsArray(foundset, ‘contactsid’);  
var query = 'SELECT contactsid FROM contacts WHERE contactsid IN (set);

```auto

as it returns an sql syntax error.

How can I convert an array (or a dataset) into the necessary format e.g. '1','2','3', etc to work as the match values for the 'IN' clause? The only saolution I have come up with so far is to loop through the array or dataset adding the commas as text:

```

var a = databaseManager.getFoundSetDataProviderAsArray(foundset, ‘contactsid’);//convert to array  
var b = ‘’;  
var c = a.length;  
for ( var i = 0 ; i \< a.length; i++ )//loop through array, adding surrounding inverted commas to values  
{

```
b += "'"+a[i]+"'";
if (i != c-1)
{
	b += ',';//add a comma for each line except the last
}

```

}

```auto

- is there a better option?
```

---

<div class="post-metadata">

**Author:** ![Harry\_Catharell](https://avatars.discourse-cdn.com/v4/letter/h/e19adc/32.png) [@Harry\_Catharell](https://forum.servoy.com/u/Harry_Catharell)\
**Post date:** [November 10, 2004, 2:02pm UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/2 "2004-11-10T14:02:37Z")

</div>

Hi David,

Need a bit more info relating to what tables you need to search on here to fully understand the search issue.

In the interim, however, I would say that SQL subquery would solve the problem for you.

Subqueries allow you to run an inner query which returns your first query foundset and then can convert this returned set into a comma delimited format to be used within the outer query which is then running the ‘IN’ clause.

Describe what you want to get and from where and I may be able to help a bit more with the syntax.

Cheers  
Harry

---

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [November 10, 2004, 3:24pm UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/3 "2004-11-10T15:24:58Z")

</div>

Harry

Thanks for the suggestion - it looks promising but..

both my select statements (i.e. the one to get the set of values for the ‘IN’ test and the one to get the set of records to match against those values) require multiple unions and I am not sure how to patch the whole thing together.

This works fine: “select contactsid from contacts where last\_name = ‘smith’ and contactsid in ()”

…but if the first statement also has multiple unions then it fails - do I have to append the sub-query to each of the unioned statements in the main select?[/code]

---

<div class="post-metadata">

**Author:** ![Harry\_Catharell](https://avatars.discourse-cdn.com/v4/letter/h/e19adc/32.png) [@Harry\_Catharell](https://forum.servoy.com/u/Harry_Catharell)\
**Post date:** [November 10, 2004, 4:43pm UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/4 "2004-11-10T16:43:28Z")

</div>

Hi David,

I am no SQL guru but I know that the general format for the subquery runs as follows and have used it successfully in testing during development:

```auto
SELECT clientid
FROM order
WHERE orderid IN
    (SELECT orderid
     FROM orderItem
     WHERE productid = '1000')

```

So within the bracket following the IN clause is the inner query and the rest is the outer query  
I know that SQL runs the inner query first which is this:

```auto
SELECT orderid FROM orderItem WHERE productid = '1000'

```

If that subquery returns two orderid’s of, say ‘1999’ & ‘2000’ then these two values are passed to the WHERE clause of the outer query comma delimited as required by the IN operator so that query now becomes:

```auto
SELECT clientid FROM order WHERE orderid IN (1999 , 2000)

```

As far as I am aware, you can add your subquery statements at any point wherever a column or literal is legal

Thus it would be perfectly logical that you would use the subquery in each Union Statement.

The subqueries would then be run first to determine that criteria passed back to finally be run under the main Union statements !

Good grief, that sounds complex to write never mind read !

I have found that a good (general) SQL resource is the on line MySQL manual at:  
[http://dev.mysql.com/doc/mysql/en/Scala … eries.html](http://dev.mysql.com/doc/mysql/en/Scalar_subqueries.html)

So this may help you make sense of the gibberish which I have posted !

Harry  
[/quote]

---

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [November 10, 2004, 5:22pm UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/5 "2004-11-10T17:22:29Z")

</div>

Actually the gibberish makes perfect sense!

Have tried this and it does work, even with multiple unions both in the sub and main queries. ![:D]( "Very Happy")

However, the other point I was trying to get across in my first post is not covered by this solution: if I have a current foundset and want to filter this set down further (again having to use sql with unions) how do I proceed. In other words I need to get the pks of the current foundset into a comma delimited list to use within an ‘in’ clause of a new sql query - is there any quick way to do this that you know of? Does this make sense?

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [November 10, 2004, 10:32pm UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/6 "2004-11-10T22:32:34Z")

</div>

best way to do it:

```auto
var set = databaseManager.getFoundSetDataProviderAsArray(foundset, 'contactsid'); 
var query = 'SELECT contactsid FROM contacts WHERE contactsid IN (';

for(var i=0;i<set.length;k++)
{
    query += '?';
    if(query.length-1 != i)
    {
       query += ',';
    }
}

quer += ')';
controller.loadData(query,set);

```

---

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [November 11, 2004, 9:45am UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/7 "2004-11-11T09:45:04Z")

</div>

Johan - tried this in a test solution:

```auto
var set = databaseManager.getFoundSetDataProviderAsArray(foundset, 'mainid'); 
var query = 'SELECT mainid FROM main WHERE mainid IN ('; 

for(var i=0;i<set.length;i++) 
{ 
  query += '?'; 
  if(query.length-1 != i) 
  { 
   query += ','; 
  } 
} 

query += ')'; 
controller.loadData(query,set);

```

but get an error ‘loadData is not a function’ - am I being stupid or am I missing something?

---

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [November 11, 2004, 10:12am UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/8 "2004-11-11T10:12:48Z")

</div>

OK, I was being stupid ![:oops:]( "Embarassed") Now realise your loadData was short hand for the whole get dataset and load/show routine!

Assume also that there were a few typos in your example, as this works fine:

```auto
var set = databaseManager.getFoundSetDataProviderAsArray(foundset, 'mainid'); 
var query = 'SELECT mainid FROM main WHERE mainid IN ('; 

for(var i=0;i<set.length;i++) 
{ 
  query += '?';
  if(set.length-1 != i) 
  { 
   query += ','; 
  } 
} 

query += ')'; 
var maxReturedRows = 10000;
var dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, set, maxReturedRows);

```

However, this still takes time to process for larger numbers of records - is there a chance that a future version could add some options to the ‘databaseManager.getFoundSetDataProviderAsArray(foundset, ‘mainid’)’ routine that would return the array already comma delimited, or even (if text values were required rather than pks) comma delimited with inverted commas round the text elements to give a list of match values such as ‘smith’, ‘jones’, ’ peters’ … This would mean a single operation to get the list instead of getting the list and then processing it as above. I don’t know if this is even possible, but it would be useful.

Thanks for the help

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [November 11, 2004, 10:35am UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/9 "2004-11-11T10:35:20Z")

</div>

sorry controller.loadRecords should be used.

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [November 11, 2004, 10:45am UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/10 "2004-11-11T10:45:37Z")

</div>

an array is an array. Wat you want is a concatting that you do now in that loop. And be aware a in query is not unlimmited!!!  
Also a query string length is not unlimmited!

---

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [November 11, 2004, 5:55pm UTC](https://forum.servoy.com/t/select-where-in-using-array-values/3039/11 "2004-11-11T17:55:13Z")

</div>

OK - just wondering! ![:?]( "Confused") I’ll stick to the loop method - it works fine.
