简体   繁体   中英

How to use sql IN clause with sql LIKE

I have spring boot application , in that in one of DAO class , we have one method where we are retrieving some data , for that we are using below query , here am using namedParameterJdbcTemplate

select student_id from student_records where create_date >= timestamp '"+appProps.getFromDate()+"' and  create_date <= timestamp '"+appProps.getToDate()+"' and student_id in (:studentIdList)

the complete method is like below

public List<String> readMatchingStudentIds_ALL(List<String> inputStudentIdsList){
            List<String> macthingStudentIdsList = new ArrayList<>();
            try{
                Map<String, List<String>> namedParameters = Collections.singletonMap("studentIdsList", inputStudentIdsList);
                String query = " select student_id from student_records where create_date >= timestamp '"+appProps.getFromDate()+"' and  create_date <= timestamp '"+appProps.getToDate()+"' and student_id in (:studentIdList)";

                macthingStudentIdsList =  namedParameterJdbcTemplate.queryForList(query, namedParameters, String.class);
            }catch(Exception e)
            {
                throw new RuntimeException(e.getMessage(),e);
            }
            return macthingStudentIdsList;
     }

everything working fine , but now problems coming , for example the incoming student is list contains "AFE1245" but in our db we have data like below "000AEF1245" , what i means 0's are prefixed , we do not know how many 0's are prefixed , for single query we can use LIKE operator like below

select student_id from student_records where student_id like '%input_student_id'

but my case is different as we need to use sql IN clause , is their any possibility we can use sql IN clause and LIKE both or is their any other way

You can do it by individual LIKEs with ORs:

SELECT student_id FROM student_records
WHERE student_id LIKE '%input_student_id'
OR  student_id LIKE 'input_student_id%'
OR  student_id LIKE '%input_student_id%';

Refer this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM