简体   繁体   English

如何使用占位符“?” 在 LIKE 运算符中?

[英]How to use placeholder '?' in LIKE operator?

I am working on a Java-based desktop application with MySQL as my back end.我正在使用 MySQL 作为后端开发基于 Java 的桌面应用程序。 In this application there is section called 'Search' where four fields are there - Key Skills (text field), Experience (combo box), City (combo box) and Notice Period (combo box).在此应用程序中,有一个名为“搜索”的部分,其中有四个字段 - 关键技能(文本字段)、经验(组合框)、城市(组合框)和通知期(组合框)。 All the four field's value will be provided at run time.所有四个字段的值都将在运行时提供。 The key skills are comma separated values.关键技能是逗号分隔值。 I want a MySQL query using LIKE operator, with the help of which I don't have to provide comma separated values every time in the Key Skills text field.我想要一个使用 LIKE 运算符的 MySQL 查询,借助它,我不必每次在关键技能文本字段中都提供逗号分隔值。 I just provide one value at run time and based on that value the query will fetch me the results using LIKE operator.我只在运行时提供一个值,并根据该值查询将使用 LIKE 运算符获取我的结果。 My query is shown below我的查询如下所示

I have used placeholder '?'我使用了占位符 '?' since my query is dynamic type.因为我的查询是动态类型。 Is it possible to use placeholder in LIKE operator or does it only work with static type query?是否可以在 LIKE 运算符中使用占位符,还是仅适用于静态类型查询?

String query = "SELECT F_Name, L_Name, Experience, Key_Skills, 
       FROM Candidate cd 
     JOIN Candidate2 cd2 ON(cd.Mobile = cd2.C_Mobile) 
      WHERE Key_Skills LIKE '%?%'"; 

When I executed the above query it produced an error stating - You have an error in your SQL syntax;当我执行上述查询时,它产生了一个错误,说明 - 您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version server version for the right syntax to use near 'FROM Candidate cd JOIN Candidate2 cd2 ON(cd.Mobile = cd2.C_Mobile) WHERE Key_Skills' at line 1检查与您的 MySQL 服务器版本服务器版本相对应的手册,以便在第 1 行的“FROM Candidate cd JOIN Candidate2 cd2 ON(cd.Mobile = cd2.C_Mobile) WHERE Key_Skills”附近使用正确的语法

You can use concat() :您可以使用concat()

String query = 
  "SELECT F_Name, L_Name, Experience, Key_Skills " + 
  "FROM Candidate cd JOIN Candidate2 cd2 ON (cd.Mobile = cd2.C_Mobile) " + 
  "WHERE Key_Skills LIKE concat('%', ?, '%')";

When you execute the above code by passing a parameter say 'someskill' the function concat() will produce this string '%someskill%' and the column Key_Skills will be compared to this concatenated string.当您通过传递参数 say 'someskill'执行上述代码时,函数concat()将生成此字符串'%someskill%'并且Key_Skills列将与此连接字符串进行比较。

Using concat you can build the like string in a simple way for managing param value:使用 concat 您可以以一种简单的方式构建like字符串来管理参数值:

   String query = "SELECT F_Name, L_Name, Experience, Key_Skills
     FROM Candidate cd 
     JOIN Candidate2 cd2 ON(cd.Mobile = cd2.C_Mobile) 
     WHERE Key_Skills LIKE concat('%' , ?, '%')"; 

NB you have a wrong comma in key_Skills, FROM - you must remove it.注意,您在key_Skills, FROM有一个错误的逗号 - 您必须将其删除。

It could be you have ambiguous column so I have updated the answer with a fully-qualified syntax.可能是您的列不明确,所以我用完全限定的语法更新了答案。

  String query = "SELECT 
        cd.F_Name
      , cd.L_Name
      , cd.Experience
      , cd.Key_Skills
     FROM Candidate cd 
     INNER JOIN Candidate2 cd2 ON cd.Mobile = cd2.C_Mobile 
     WHERE cd.Key_Skills LIKE concat('%' , ?, '%')"; 

You can do the prefix and suffix in the Java code instead of concatenating them in SQL.您可以在 Java 代码中使用前缀和后缀,而不是在 SQL 中将它们连接起来。

WHERE Key_Skills LIKE ?"; 

Maybe even allowing wild cards:甚至可能允许通配符:

keySkills = keySkills.replace('*', '%');
keySkills = keySkills.replace('?', '_');

preparedStatement.setString(1, "%" + keySkills + "%");

The JDBC code will replace ? JDBC 代码将取代? with a prepared statement value, coerced to the expected SQL string type.带有准备好的语句值,强制为预期的 SQL 字符串类型。 And setString takes care of escaping apostrophes and others.setString负责转义撇号等。

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

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