Using Postgresql and Spring.
The Backend gets User Input from the frontend which is called "filter". I want the database "contacts" to be checked in multiple columns:
@Query("SELECT c FROM Contacts c " +
"WHERE LOWER(CAST(c.supplierNo AS string)) LIKE %:filter% OR LOWER(c.firstName) LIKE %:filter%" +
"OR LOWER(c.lastName) LIKE %:filter% OR LOWER(c.supplierName) LIKE %:filter%")
List<Contacts> findByFilter(String filter);
This works for single-word-filters. But I would want the user to enter for example: "John Smith"(firstName SPACE lastName), or maybe Smith Foodmarket(lastName SPACE supplierName) or Foodmarket Smith. You get the idea. How would I go about this?
WHAT I TRIED:
1
Find search term in multiple columns
This is not it, since I want to be able to search in multiple cols at once.
2
Search Single Value In Multiple Columns
I tried this like shown:
@Query("SELECT c FROM Contacts c " +
"WHERE LOWER(CAST(c.supplierNo AS string)) LIKE CONCAT('%', :filter, '%') OR LOWER(c.firstName) LIKE CONCAT('%', :filter, '%')" +
"OR LOWER(c.lastName) LIKE CONCAT('%', :filter, '%') OR LOWER(c.supplierName) LIKE CONCAT('%', :filter, '%')")
List<Contacts> findByFilter(String filter);
It is entirely possible that I translated sql -> jpql the wrong way, but this solution does not work as intended
3
Multiple permutations of this, but to no success.
2
Answers
The problem with your query is that if you pass string which contain more than 1 word then all of your conditions will return FALSE
try this
SQL code that i tested
You can improve your query for 2 words case.
If filter contains 1 word:
filter1=word
filter2=word
If filter contains 2 words:
filter1=word1
filter2=word2
Updated query