简体   繁体   English

如何在SQL查询的like子句中使用占位符?

[英]How to use placeholders in like clause in SQL query?

This is my query:这是我的查询:

 select * from Table1 where Identifier = 'ABC' and Identifier_Type like ':name%:id' 

can I change this to,我可以改成这样吗

 select * from Table1 where Identifier = 'ABC' and Identifier_Type like '?%?'  

will it work?它会起作用吗? I have to use these parameters in the prepared statement so I thought of replacing the named parameters with the placeholders '?'我必须在准备好的语句中使用这些参数,所以我想用占位符'?'替换命名参数'?' .. will it work like this '?%?' ..它会像这样工作'?%?'

Query Should be like this::查询应该是这样的::

    PreparedStatement pstmt = con.prepareStatement ("Select * 
    from 
    Table1 where Identifier = 'ABC' and Identifier_Type  like?");

While setting in prepared statement:在准备好的语句中设置时:

   pstmt.setString(1, "%" + data + "%"); // where data is string Variable

OR By using SQL function或 通过使用 SQL 函数

   PreparedStatement pstmt = con.prepareStatement(
  "select * from Table1 where Identifier = 'ABC' and Identifier_Type  
    like  CONCAT( '%',?,'%')";
     
     pstmt.setString(1, data); // Where data is String variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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