Advertisement
edigitman

Query by name order

Mar 15th, 2025
180
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | Software | 0 0
  1. Which one better prioritize the results start with the searched string ?
  2.    
  3. Version 1:
  4. @Query("Select org from Organisation org " +
  5.        "WHERE org.status in ('ACTIVATED', 'SUSPENDED') " +
  6.          "AND (UPPER(org.orgName) LIKE UPPER('%' || :name || '%') OR org.identificationCode LIKE %:code% ) " +
  7.          "AND (:interest IS NULL OR org.interestRepresented = :interest)" +
  8.        "ORDER BY CASE " +
  9.           "WHEN UPPER(org.orgName) LIKE UPPER(CONCAT(:name, '%')) THEN 1 " +
  10.           "WHEN org.identificationCode LIKE CONCAT(:code, '%') THEN 2 " +
  11.           "ELSE 3 END")
  12.  
  13.  
  14. Version 2:
  15. @Query("SELECT org from Organisation org " +
  16.        "WHERE org.status in ('ACTIVATED', 'SUSPENDED') " +
  17.          "AND (LOCATE(UPPER(:name), UPPER(org.orgName)) > 0 OR LOCATE(:code, org.identificationCode) > 0) " +
  18.          "AND (:interest IS NULL OR org.interestRepresented = :interest)" +
  19.        "ORDER BY LOCATE(UPPER(:name), UPPER(org.orgName)) + LOCATE(:code, org.identificationCode)")
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement