MySQL的prepare用法主要有以下幾種:
PREPARE stmt FROM 'SELECT * FROM table WHERE column = ?';
SET @param = 'value';
EXECUTE stmt USING @param;
DEALLOCATE PREPARE stmt;
SET @param = 'value';
PREPARE stmt FROM 'SELECT * FROM table WHERE column = ?';
EXECUTE stmt USING @param;
SET @param1 = 'value1';
SET @param2 = 'value2';
PREPARE stmt FROM 'UPDATE table SET column1 = ? WHERE column2 = ?';
EXECUTE stmt USING @param1, @param2;
需要注意的是,使用PREPARE語句時(shí),占位符必須使用問號(?)來表示。而使用EXECUTE語句時(shí),可以使用用戶定義的變量來替代占位符。