如何使用like操作符编写mysql存储过程

tzcvj98z  于 2023-03-28  发布在  Mysql
关注(0)|答案(4)|浏览(93)

下面是我的代码:

DELIMITER $$

     CREATE DEFINER=`root`@`localhost` PROCEDURE `employee_with_asset`(IN name     VARCHAR(250))
     BEGIN
     SELECT a.Asset_code,a.name as name1,a.type,a.description,`purchase date`,
    `amc availability`,`amc renewal`,`employee Id`,b.Name FROM `asset_details` a,
     employee b WHERE  b.Name LIKE '%' + @name + '%' and a.`assigned to`=b.`employee Id`;
     END

在LIKE附近显示错误。如何解决。

9njqaruj

9njqaruj1#

mysql中的连接是使用CONCAT()完成的

LIKE CONCAT('%', @name , '%')

完整声明

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `employee_with_asset`
(
    IN _name     VARCHAR(250)
)
BEGIN
    SELECT  a.Asset_code,
            a.name as name1,
            a.type,
            a.description,
            `purchase date`,
            `amc availability`,
            `amc renewal`,
            `employee Id`,
            b.Name 
    FROM    `asset_details` a
            INNER JOIN employee b 
                ON a.`assigned to` = b.`employee Id`
    WHERE   b.Name LIKE CONCAT('%', _name , '%');
END $$
DELIMITER ;
h79rfbju

h79rfbju2#

吴宇森的答案是正确的,但如果你不使用utf8_general_ci集合,你必须在WHERE子句之后(如果你有多个条件,则在每个条件之后)将集合转换为与你比较的列的集合相同,就像这样

... WHERE name LIKE CONCAT('%', @name , '%') COLLATE utf8_unicode_ci

或多种条件

... WHERE name LIKE CONCAT('%', @name , '%') COLLATE utf8_unicode_ci
OR job LIKE CONCAT('%', @name , '%') COLLATE utf8_unicode_ci ...
nzk0hqpo

nzk0hqpo3#

PHP语言:

$keyword = $_POST['keyword'];
$response = "call user_search('%$keyword%');";

我的数据库:

....where user.name like @keyword;
c8ib6hqw

c8ib6hqw4#

我在这篇文章中找到的解决方案Re:向LIKE操作符传递一个参数。我需要像字符串一样创建一个SQL语句,然后执行该语句。

-- DROP PROCEDURE IF EXISTS db1.SelectEmpleados;

CREATE PROCEDURE SelectEmpleados(IN var varchar(100) )
BEGIN
    
    SET @qry = CONCAT('SELECT * FROM empleados WHERE CONCAT(nombre, \' \',apellido) LIKE \'%', var, '%\'');
    PREPARE stmt FROM @qry;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END

-- CALL SelectEmpleados();

对于我来说,在MariaDB 10.4.27上不工作:

WHERE name LIKE CONCAT('%', @name , '%')
WHERE name LIKE CONCAT('%', name , '%')
WHERE name LIKE CONCAT('%', "@name" , '%')
WHERE name LIKE CONCAT('%', @"name" , '%')
WHERE name LIKE '%'+name+'%'

相关问题