I've been using single select components so far with no problem, but now I'm trying to use a multiselect I run into trouble.
I've gone back to trying a simple example with the syntax I've seen in a few other posts...
SELECT Item_Id FROM tbl_Items WHERE Item_Id = ANY ({{ multiselect1.value }});
The code window shows me the current value of the multiselect as...
But when I Preview the code I get...
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '24, 25, 23, 594 )' at line 2"
I see that "ANY" is mainly used with the results of a subquery, but I haven't found a way to make it accept the result of the multiselect.value.
Thanks for the suggestion but it doesn't seem to accept STRING_SPLIT.
message:"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'STRING_SPLIT ( '594,25,24,23' , ',' ) )' at line 5"
To start with it seems my mariadb doesn't support the "= ANY" comparison, so I switched to "IN" and that works fine for manual queries where I log into my server directly (ie outside Retool)...
SELECT Item_Id FROM tbl_Items WHERE Item_Id IN (24,23,25,594);
I donβt know MariaDB well, but did you try IN with the STRING_SPLIT code? For IN, you need to pass a string of strings (β1β,β2β,β3β) not a string made of strings (β1,2,3β).
# OR tI.Item_Id IN ({{ (multiselect1.value.flat()) }})
OR tI.Item_Id IN ( 774, 782 )
# OR tI.item_Id IN ({{ multiselect1.value.map(item => `"${item}"`).join(',') }})
OR tI.item_Id IN ( '\"774\",\"782\"' )
Looking carefully you can see that the "map join" version ends up with single quotes around the whole string.
So I'll stick to the one that works - multiselect1.value.flat()
Now that I have multiselect working I have found a small "snag".
I have a default value set, but since all selected items have a small "x" next to them to allow removal, it is now possible to de-select all items so the multiselect is completely empty.
Then the query is sent with an empty "IN () " which causes an error response.
You should be able to set the Disable Query (only run when)condition in the advanced tab of the query with something like "don't run if list is empty". You can use similar logic to disable whatever triggers the query as well.
Depending on what you are getting on the server you can try:
{{
multiselect1.value.flat() == undefined
// or multiselect1.value.flat().length == 0 if the return is an empty array []
? '0' // whatever you want your fallback to be
: multiselect1.value.flat() //otherwise just give the values
}}
To your earlier question, using Postgres, the syntax is = ANY( {{ array }} ) with no need to fiddle with the array. Like you, I don't understand why .flat() on a flat array makes any difference...
p.s. @pyrrho's approach is also good if you just don't want it to fire the query
Interestingly, in a similar situation, where I just changed a table from single selected row to multiple selected rows, the basic array from selectedRowKeys...
WHERE tPbsub.Package_booking_Id IN ({{ table19.selectedRowKeys }})
...works fine and at the server I get the keys with no single quotes around them.