簡體   English   中英

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

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

這是我的查詢:

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

我可以改成這樣嗎

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

它會起作用嗎? 我必須在准備好的語句中使用這些參數,所以我想用占位符'?'替換命名參數'?' ..它會像這樣工作'?%?'

查詢應該是這樣的::

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

在准備好的語句中設置時:

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

或 通過使用 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