如何使用类似条款在MySQL 5.0中声明(How to use like clause in My

2019-09-29 06:07发布

我一直在使用“喜欢”条款在MySQL 5.0我在MySQL 5.0中写了一个存储过程,并从我的Java程序下面的存储过程中的SELECT语句中调用存储过程我想利用参数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 ;

Answer 1:

当您附上departmentname引号,就变成了字符文字,并且不再是一个变量。 它的价值被忽略,文字'%departmentname%'来代替。

最简单的办法是使用CONCAT添加'%'作为前缀和后缀,就像这样:

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

如果,例如, departmentname设置为police ,结果变成

and DeptName LIKE '%police%'


Answer 2:

格式:

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

例如:

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


文章来源: How to use like clause in MySQL 5.0 Statement