简体   繁体   English

如何在MySQL 5.0语句中使用like子句

[英]How to use like clause in MySQL 5.0 Statement

I have a problem using 'like' clause in MySQL 5.0 I have written a stored procedure in MySQL 5.0 and calling the Stored Procedure from my Java Program the stored procedure below in the select statement i want to take the value of parameter departmentname for like clause please Help with this 我在MySQL 5.0中使用'like'子句时遇到问题我在MySQL 5.0中编写了一个存储过程,并从我的Java程序中调用了存储过程,在select语句中使用了下面的存储过程,我想为like子句获取参数Departmentname的值请帮助这个

    DROP PROCEDURE IF EXISTS upSelTests;
    DELIMITER //
    CREATE PROCEDURE upSelTests
    (
    IN departmentname varchar(50),
    IN testDateFrom varchar(10),
    IN testDateTo varchar(10)
    )
    BEGIN
    declare testDateFrom1 varchar(30);
    select PatientID,LabNo,arriveddate,RecieptNo,patientname,referredby,ID from
    tblAcceptTest where STR_TO_DATE(arriveddate, '%d/%m/%Y')>=STR_TO_DATE(testDateFrom,
    '%d/%m/%Y') and STR_TO_DATE(arriveddate, '%d/%m/%Y') <= STR_TO_DATE(testDateTo, 
    '%d/%m/%Y') and Status = 0 and DeptName like '%departmentname%';
    END //
    DELIMITER ;

When you enclose departmentname in quotes, it becomes a character literal, and ceases to be a variable. 当您将departmentname括在引号中时,它将成为字符文字,并且不再是变量。 Its value is ignored, and the literal '%departmentname%' is used instead. 其值将被忽略,而改用文字'%departmentname%'

The simplest solution is to use CONCAT to add '%' as a prefix and a suffix, like this: 最简单的解决方案是使用CONCAT添加'%'作为前缀和后缀,如下所示:

and DeptName LIKE CONCAT( '%', departmentname, '%')

If, for example, departmentname is set to police , the result becomes 例如,如果departmentname名称设置为police ,结果将变为

and DeptName LIKE '%police%'

Format: 格式:

SELECT field1, field2,...fieldN table_name1, table_name2...
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'

Eg: 例如:

SELECT * from tutorials_tbl 
 WHERE tutorial_author LIKE '%jay';

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

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