MySQL/MariaDB NOT IN Statement Not Working With Array

Wappler Version : 6.3.3 stable
Operating System : Win11
Server Model: NodeJS
Database Type: MariaDB
Hosting Type: Dedicated Server (Windows)

Expected behavior

There are 16 UIDs in total inside a table. The array: arrUIDs shows 10 UIDs that have courses listed next month. 6 do not and are not in the array.

A list of 6 UIDs should be shown after being filtered to show UIDs that are not in the array arrUIDs .

Actual behavior

15 records are shown instead of 6, it looks like the WHERE UID NOT IN (:P1) is only selecting the first record

How to reproduce

Query that pulls out the UIDs with courses next month:

SELECT  DISTINCT CID FROM thedb.courses 
WHERE MONTH(StartDate)  = MONTH(NOW() + INTERVAL 1 MONTH)

Create a flattened array to pull out the UIDs from above

image

The array pulls out the correct UIDs:
11081,11095,12233,6040,7995,9172,9017,7074,11085,12245

Array is then used to filter the database using a custom and a simple query.

The custom query shows 15 records instead of 6:

SELECT UID, Company, CompEmail, ContactName FROM thedb.ropecompanies 
WHERE UID  NOT IN (:P1) 
AND Trainer = 'Yes' 
AND AccountLevel >0 

Heres the parameter for the above:

The simple query triggers an error:

image

Iā€™ve tried a number of variations of the above and its been peer tested in a few different ways in this thread (the steps are different below as it uses a list array but the results are the same):

Hey Tom,

Have you tried the result on your browser or just from the query builder notification?

Iā€™m saying that because I have experencied such error Notifications while on query builder but when I finally test on browser it worksā€¦ Obviously query builder cannot manage some statements
Just saying my friendā€¦

1 Like

That was well worth a try, hereā€™s what is returned on the front end:
image

1 Like

Tom, on simple query you need to join and split the values not use the array itself

Check the join and split and test in browser

arrUIDs.join(ā€™,ā€™, ā€˜ITEMā€™).split(ā€™,ā€™)
Where ā€œITEMā€ is the item of the array to filter

You cannot use IN and NOT IN like that directly in a custom query.
This has been discussed in a few topics already, for example GET array for use with IN clause of custom query action

2 Likes

Thanks @Teodor

Thanks for that @Teodor @famousmag and @franse your collective assistance is much appreciated.

For anyone else reading, this was my solution:

SELECT UID, Company, CompEmail, ContactName FROM thedb.companies 
WHERE  NOT FIND_IN_SET(UID, :P1) 
AND Trainer = 'Yes' 
AND AccountLevel >0 
2 Likes