I am developing job portal similar and I had postgresql table job_seeker with column name skill holds the job seeker skills as comma separated values
table name - job_seeker
columns - id name skills
values(data) -1 mahesh java,spring,mysql
2 rakesh html,css,jsp
Used spring boot with rest controller and from UI skills queryparam getting as a list to search job seeker based on multiple skills.
JobSeekerRepository.java
@Query(value="SELECT * FROM job_seeker WHERE skills in (:skills)", nativeQuery = true)
public Page<JobSeeker> getAllJobSeekerProfileBySkills(@Param("skills") List<String> skills, Pageable pageable);
Above query will works only if skills column has a single value but it contains comma seperated values. I need to use like statement for each value in the skills column for the same record.
Example:-
In the above example, if input list of skills has css and spring then query should return two records and if input list has only html then second record should fetch.
Can you please suggest me how to write the query to get work as per requirement.
2
Answers
You could use recursive cte to transpose your skills list into rows and then join cte to your table selecting just persons having any skill from list of skills provided (asked for):
… here is recursive cte named skills …
… next is just testing the cte resultset …
This is the main Select that combines your original table data with recursive cte skills data from the test above…
… result for (‘spring’, ‘css’) – you could ask for any list of skills …
You may convert both the search and target comma separated values into arrays using string_to_array and then check if these arrays overlap using the && operator.
However as already noted this is not a good data design. Having
skills
astext[]
or having a normalized data design (the best choice) would be far better.See demo.