我该怎么做?
CREATE DEFINER=`root`@`localhost` PROCEDURE `MyProcedure`(
IN param VARCHAR(50))
BEGIN
SELECT id, column2, column3
FROM table1
WHERE column5 = param;
LIMIT 1;
IF (FOUND_ROWS() > 0) THEN
UPDATE table1 SET column4 = 1 WHERE id = theIdFromRowAbove;
END IF;
END
我希望我的存储过程从table1中选择一行,如果找到一行,则更新table1,其中id =找到的行中的id。我一直在搜索互联网,但仍然无法弄明白。我不确定要搜索什么。
答案 0 :(得分:1)
您可以在查找行设置为true
后获取变量并将其值设置为falseCREATE DEFINER=`root`@`localhost` PROCEDURE `MyProcedure`()
BEGIN
DECLARE find_row INT DEFAULT 0;
DECLARE theIdFromRowAbove INT DEFAULT 0;
SELECT id, column2, column3, 1 INTO theIdFromRowAbove, @column2, @column3, find_row
FROM table1
WHERE id = 5
LIMIT 1;
IF (find_row) THEN
UPDATE table1 SET column4 = 1 WHERE id = theIdFromRowAbove;
END IF;
END
了解有关SELECT....INTO
Official Link