Page 1 of 2
SQL WHERE statement optional
Posted: Thu Aug 21, 2025 2:01 pm
by Uzlander
Hi people! Probably this is a beginner level question but i couldn't make the WHERE statement optional.
Context: The second (select type )input gets filtered based on the first input, but if the first left blank (empty|null|undefined) then no filter for the second input should work.
So
Code: Select all
WHERE
((rblocks.kompleks = '#KOMPLEKS#'))
clause somehow doesn't make it really optional and filter's working anyway, whereas i need WHERE eliminated if '#KOMPLEKS#' equals null and all records should show up
Re: SQL WHERE statement optional
Posted: Thu Aug 21, 2025 2:11 pm
by kev1n
Maybe like this?
Code: Select all
WHERE (
IFNULL('#KOMPLEKS#','') = ''
OR LEFT('#KOMPLEKS#',1) = '#'
OR rblocks.kompleks = '#KOMPLEKS#'
)
Explanation line by line
IFNULL('#KOMPLEKS#','') = ''
– If #KOMPLEKS#
is NULL
or empty, the condition is true.
– Meaning: Don’t filter, show all records.
LEFT('#KOMPLEKS#',1) = '#'
– If the first character of #KOMPLEKS#
is #
, the condition is true.
– This usually indicates the hash cookie placeholder wasn’t replaced.
– Meaning: Ignore filter, show all records.
rblocks.kompleks = '#KOMPLEKS#'
– If #KOMPLEKS#
contains a valid value, only rows where rblocks.kompleks
matches it will be returned.
Re: SQL WHERE statement optional
Posted: Wed Oct 01, 2025 4:31 pm
by Uzlander
I think there's a problem with retrieving records thus priorly saved. I mean having saved the record, then reopen it (either browse or edit purposes) it just replaces the aforesaved value with the first one it manages to get from the database field, cos (seemingly) there is no #KOMPLEKS# hash exists at that moment. So effectively the value is lost.
And i need to retrieve the existing value if there was any saved before, and only in case the field's originally empty or i'm dealing with new record it should fire the WHERE filter above.
I can publish short screencast if needed..
Re: SQL WHERE statement optional
Posted: Wed Oct 01, 2025 8:45 pm
by steven
Uzlander,
If you break the SQL statement on purpose eg. 1WHERE ......
You will get an error in nuDebug and it will show the actual SQL that is being run with its WHERE clause.
Steven
Re: SQL WHERE statement optional
Posted: Thu Oct 02, 2025 2:32 pm
by Uzlander
This is not about an error. I basically want to implement dependent select lists (dropdown select - feels fitting). Using conditional WHERE statement in SQL i get the second list filtered if HasgCookie's set, and the whole stack if no HasCookie.
I played a bit and figured it does its job good until saved, but after re-opening the same record it doesn't always display what's expected.
https://jumpshare.com/share/7A0SuAOT5FNzr0Wp2y4v
Re: SQL WHERE statement optional
Posted: Thu Oct 02, 2025 2:37 pm
by Uzlander
So the first two elements are select dropdowns (the second one depends on the first) and the third one is display type, which in turn doesn't behave consistently. The Link ->
https://i.postimg.cc/vBYBK7Jy/dogov-draft2.jpg
I can post an sql database backup if required, there's no real data in it yet
Re: SQL WHERE statement optional
Posted: Thu Oct 02, 2025 2:39 pm
by kev1n
Please post your db dump.
Re: SQL WHERE statement optional
Posted: Thu Oct 02, 2025 4:40 pm
by Uzlander
Dump/backup attach
So i think it boils down to simpler question: what is the recommended way to implement dependent select fields (either lookup or dropdown), i couldn't find it in the docs ?
Thanks a lot !:)
Re: SQL WHERE statement optional
Posted: Fri Oct 03, 2025 3:58 am
by kev1n
In your second select object (dogov_kvart), you can simply reference the first one like this:
Code: Select all
SELECT
kvart.kvart_hid,
UPPER(kvart.kvart_hid)
FROM
kvart
WHERE
(
(LEFT('#dogov_blok#',1) = '#')
OR
(kvart.blok_dd = '#dogov_blok#')
)
I don't see the need to set a separate hash cookie in dogov_blok.
There’s also a demo showing this approach here:
https://demo.nubuilder.cloud/
Re: SQL WHERE statement optional
Posted: Fri Oct 03, 2025 9:28 am
by Uzlander
Oh, before i check the code above in action, how is it gonna work? I mean there isn't such HashCookie as '#dogov_blok#' user set, or does it work as the actual field's value holder variable by default ?