Hi,
is it possible to change the SQL code of a select object of an edit form, according if it is a new record or an existing one?
If yes, which is the best way?
Thanx
Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
SQL of select object in edit form
-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: SQL of select object in edit form
Hi,
Depending on how different the SQL statements are, here are two possibilities.
1. Using UNION:
In the first SQL statement, a UNION is used to combine the result sets of two SELECT statements. The first SELECT statement returns the value of the 'some_column' where '#RECORD_ID#' is equal to '-1'. The second SELECT statement returns the value of the 'another_column' where '#RECORD_ID#' is not equal to '-1'. This method can be useful when the two SELECT statements are significantly different and cannot be combined using an IF statement.
2. USING iF:
In the second SQL statement, an IF statement is used to determine which column to select based on the value of '#RECORD_ID#'. If '#RECORD_ID#' is equal to '-1', then 'some_column' is selected, and if it is not equal to '-1', then 'another_column' is selected. This method can be useful when the two columns are very similar, and it is more efficient to use an IF statement rather than two separate SELECT statements.
#RECORD_ID# is a Hash Cookie and will be replaced with the current record ID. -1 means it's a new record.
Depending on how different the SQL statements are, here are two possibilities.
1. Using UNION:
Code: Select all
SELECT some_column FROM your_table WHERE '#RECORD_ID#' = '-1'
UNION
SELECT another_column FROM your_table WHERE '#RECORD_ID#' <> '-1'
2. USING iF:
Code: Select all
SELECT IF('#RECORD_ID#' = '-1', `some_column`, `another_column`) FROM your_table
#RECORD_ID# is a Hash Cookie and will be replaced with the current record ID. -1 means it's a new record.
-
- Posts: 64
- Joined: Sun Jan 22, 2023 7:03 pm
-
- Posts: 64
- Joined: Sun Jan 22, 2023 7:03 pm
Re: SQL of select object in edit form
Update.
I found another working solution, using:
I found another working solution, using:
Code: Select all
CASE WHEN '#RECORD_ID#' = '-1'
THEN things to do if it is a new record
ELSE things to do if it is an existing record
END
-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: SQL of select object in edit form
Yes, that works as well.
In MySQL, both CASE WHEN and IF() are used to conditionally execute code. However, they differ in syntax and usage:
CASE WHEN is used for complex conditional expressions with multiple conditions. It is similar to a switch statement in other programming languages.
while IF() is used for simple conditional expressions with only two outcomes.
In MySQL, both CASE WHEN and IF() are used to conditionally execute code. However, they differ in syntax and usage:
CASE WHEN is used for complex conditional expressions with multiple conditions. It is similar to a switch statement in other programming languages.
while IF() is used for simple conditional expressions with only two outcomes.