如何在 mysql 中使用 LIKE 子句使用 vb.net 与不同的字符串输入进行比较?

How to query using LIKE clause in mysql using vb.net comparing to a varied string input?

我已经搜索并尝试了很多程序,但都没有用。 CREATE PROCEDURE 不起作用,SET @variable 不是 acceptable 和更多代码。这是我的最后一个 "query" 代码,但仍然无效。

qry = "select * from employeefile where empfname LIKE '%''" + input + "''%'"

empfname 是来自 table 员工文件的员工姓名,可能由三个单词组成。输入可以是第一个词、第二个词、第三个词或整个词。

当我尝试输入名称中的任何单词时,程序仍然会提示,"no records found."当我尝试将查询更改为

qry = "select * from employeefile where empfname LIKE '%existingname%'"

并且我的输入是 "existingname",程序运行就像我想要的那样。

这段代码是我搜索过但仍然无效的代码之一。

T-SQL and the WHERE LIKE %Parameter% clause

How to use like clause in MySQL 5.0 Statement

T-SQL and the WHERE LIKE %Parameter% clause

这里的问题是当我使用一个变量时...我可能以错误的方式将它用于查询。请帮我。顺便说一句,我是新来的。

如果我没有正确理解你的问题:

Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE '%' + @empfname + '%'", connection)

command.Parameters.AddWithValue("@empfname", input)

或:

Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE @empfname", connection)

command.Parameters.AddWithValue("@empfname", "%" & input & "%")

您必须在 SQL 代码或 VB 代码中将通配符与输入文本连接起来。

这是正确的语法:LIKE '%blah%',但是这段代码没有将引号放在 %s 之外:LIKE '%''" + input + "''%'".

我得到了答案。事实证明我只是过度使用了单引号。必须这样写:

qry = "select * from employeefile where empfname LIKE '%" + input + "%'"