SQl存储过程无法正常工作

时间:2014-08-25 08:19:33

标签: mysql sql

我有一个无法正常运行的存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `visitor_search`(
IN _startDate datetime,
IN _endDate datetime,
IN _visitorName text,
IN _personToVisit text
)
BEGIN
SELECT * FROM visitor
where
logged_in >= _startDate
and logged_in <= _endDate
and concat(firstname, "", lastname) like('%' + _visitorName + '%')
and person_to_visit like('%' + _personToVisit + '%')
order by logged_in desc;
END

我认为以下几行正在起作用。

and concat(firstname, "", lastname) like('%' + _visitorName + '%')
and person_to_visit like('%' + _personToVisit + '%')

这是正确的语法吗?

此致

鲍勃

2 个答案:

答案 0 :(得分:2)

您也需要使用CONCAT()

and concat(firstname, "", lastname) like concat('%', _visitorName, '%')
and person_to_visit like concat('%', _personToVisit, '%')

并且不要忘记使用不同的分隔符。

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `visitor_search`(
IN _startDate datetime,
IN _endDate datetime,
IN _visitorName text,
IN _personToVisit text
)
BEGIN
SELECT * FROM visitor
where
logged_in >= _startDate
and logged_in <= _endDate
and concat(firstname, "", lastname) like concat('%', _visitorName, '%')
and person_to_visit like concat('%', _personToVisit, '%')
order by logged_in desc;
END $$
DELIMITER ;

答案 1 :(得分:0)

尝试这个:

and concat(firstname, "", lastname) like concat('%' , _visitorName , '%')
and person_to_visit like concat('%' , _personToVisit , '%')

LIKE不是一个功能