如何在 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
这是我的查询:
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