Help with a SQL where / and is based on input or dropdown selections

Search for Notes and limit the search based on selection and values.

My issue is linkted to the MySQL query's and the WHERE clause

User have 4 field user can use to limit the search:

  • Contact name (dropdown list)
  • Contact Company (dropdown list)
  • Type (dropdown list)
  • Note (textinput)

I want the Main SQL to use WHERE close when applicable

SELECT *
FROM note
WHERE Note  LIKE %{{textInput}}%
AND (  {{SelectCompany.selectedLabel.length > 0 }}
            AND NoteCompanyID = {{SelectCompany.selectedItem.Cid}}
         )
AND (  {{SelectUser.selectedLabel.length > 0 }}
       AND NoteUserID = {{SelectUser.selectedItem.Uid}}
          )
AND ( {{SelectType.selectedLabel.length > 0 }}
        AND  NoteTypeID = {{SelectUser.selectedItem.Tid}}
         )
GROUP BY NoteID

List of users in my DropDown User list needs to be limited based on Selected Company.

My sql query to get users:

SELECT UserID, UserFullName
FROM   users 
WHERE UserID > 0
AND 
{{SelectCompany.selectedLabel.length > 0 ?
UserCompanyID = SelectCompany.selectedItem.UserCompanyID : '1=1'}}
GROUP BY UserID

Non of the querys above is working. I do not know how to do this and was hoping that somebody in the community can help me?

I do not have much knowledge about IF in MySQL or JavaScrip and need help. with ssolvig this?

Hello @Thore!

A couple of things that I think can help get you up and running with this query:

  1. In your first query, the WHERE Note LIKE %{{textInput}}% is a little off -- you'll need the value from the textInput, not just the component and you are going to want to include the wildcard % as part of the {{ }} like this:

WHERE Note LIKE {{'%' + textInput.value +'%'}}

I recommend testing this query without the addtional AND conditions to make sure the results come back when there is data in the textInput component

  1. Once the WHERE clause is working with just this one condition I would then start adding back the rest and testing them in stages. I believe the way you are returning the truthiness of the component values will work, but using the Label length might not be the most logical choice -- just a thought, not an issue.

Once the entire query is returning values for each of your components, address the next query:

  1. Here it looks like you are trying to return a value conditionally. For SQL, you would want to use the IF/ELSE syntax:
...
AND
IF {{SelectCompany.selectedLabel.length > 0}}
   UserCompanyId = {{SelectCompany.selectedItem.UserCompanyID}};
ELSE
    1=1;

Keep in mind that you can also use logic a little more cleanly in a JavaScript transformer which creates a fully formed statement for you to use in the query you are making. This can help if you are more comfortable outside of SQL but know how to piece together the statements you need.

Let us know if the query suggestions above don't work out or you get stuck with a new issue if they do start to work :slight_smile:

Hi @pyrrho

Thanks for this.

On the first query I have multiple filters for user to select from and limit the search.

SQL shall return all records if user only press the Search button. Else the result shall be limited based on selection or text inputed.

`SELECT *
FROM note
WHERE NoteText LIKE {{'%' + SearchNote.value + '%'}}

The above SQL return all if SearchNote.value is empty or 644 rows with SearchNote = test.

SELECT *
FROM note
WHERE (  {{SearchNote.value.length>0}}
          AND NoteText LIKE {{'%' + SearchNote.value + '%'}} )

AND (  {{SelectCompany.selectedLabel.length > 0 }}
            AND NoteCompanyID = {{SelectCompany.selectedItem.Cid}}
         )
AND (  {{SelectUser.selectedLabel.length > 0 }}
       AND NoteUserID = {{SelectUser.selectedItem.Uid}}
          )
AND  ( {{SelectType.selectedLabel.length > 0 }}
        AND  NoteTypeID = {{SelectUser.selectedItem.Tid}}
         )
GROUP BY NoteID

The SQL above returns 0 rows if all is empty or if SearchNote = test.

I hope you can see any errors or recommend a way to sovle the issue to have 4 different input fields.

-Thore.

1 Like

In this group of conditions, it seems like you check a component for a Label with a non-zero length and matching property. In the first two conditionals, the check for length is done on 3 different components, but in the last one the property check is done against SelectUser.selectedItem.Tid seemingly not from the SelectType component -- should this be SelectType.selectedItem.Tid?

Hi @pyrrho

On the last AND it is a ID as long as I did not select any in the dropdown list then it is empty. It shall not matter?

I also used ChatGPT and it still does not work, below is the last updated WHERE Clouse I have tested.

WHERE 
  ({{ SearchNote.value ? Note LIKE '%` + SearchNote.value + `%'` : '1=1' }})
 AND
   ({{ Select_ContactName.selectedLabel ? `CompanyContact = ` + Select_ContactName.selectedItem.UserID : '1=1' }})
 AND
   ({{ SelectContactCompany.selectedLabel ? `CompanyId = ` + SelectContactCompany.selectedItem.CompanyID : '1=1' }})
 AND
   ({{ SelectManuf.selectedLabel ? `TypeID = ` + SelectManuf.selectedItem.tid : '1=1' }})

I expected this to work when I have Note LIKE '%test%' and all other is blank. As you can see it result in "No rows found"

If i use the following code:

WHERE Note LIKE {{'%' + SearchNote.value + '%'}}

image
Is shows 645 result.

Is it due to use of Label?

Possibly! You may need to actually check the label for a condition (like != '' || != null || != undefined) since it is technically always present on the component and will always go down the truthy path.

Hi @pyrrho

I Have tested multiple ways and used ChatGPT without solving the issue with SQL in ReTool.

Can be related to ... LIKE '%%' when the note field is empty.

I have removed all filters and added the search fuction on the Table. Users can use this with the filter on table footer.

Thanks for the time and help you provided.

-Thore

2 Likes