DELIMITER $
DROP PROCEDURE IF EXISTS getInfo $
CREATE PROCEDURE getInfo(IN ids VARCHAR(4000))
BEGIN
DECLARE wherestatement NVARCHAR(500);
SET wherestatement="where 1=1 ";
IF (ids IS NOT NULL) OR (LTRIM(ids) != '') THEN
SET wherestatement=CONCAT(wherestatement," and id in (",ids,")");
END IF;
SET @sqlinfo = CONCAT("select * from student ",wherestatement);
PREPARE stmt FROM @sqlinfo;
EXECUTE stmt;
END $
CALL getInfo("1,2,3,4");进行查询,数据库中只有1,2两条记录,就会将其查出