SQL query filtering by list of parameters

后端 未结 3 1809
悲&欢浪女
悲&欢浪女 2021-01-19 02:13

I have a query where I want to return all the rows which are associated with a list of values. You could write this very simply as:

select * from TableA whe         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 02:48

    You can easily write this:

    String csvString = "1, 2, 3, 5"; // Built the list somehow, don't forget escaping
    String query = "select * from TableA where ColumnB in (" + csvString + ")";
    

    By this way, performance doesn't decreased, and you can prevent Sql Injection simply escaping input values while creating csvString.

    BTW, if you use MS SQL instead of standard SQL, you can find alternative ways.

提交回复
热议问题