我們可以建立一個帶有 IN和OUT參數的預存過程,從MySQL表中取得多個值。為了讓它理解,我們以一個名為「student_info」的表為例,該表包含以下資料−
mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Bangalore | Computers | +------+---------+------------+------------+ 4 rows in set (0.01 sec)
現在,透過建立以下名為'select_studentinfo'的過程,我們可以透過提供'id '的值從'student_info'表中選擇值−
mysql> DELIMITER // ; mysql> Create Procedure Select_studentinfo ( IN p_id INT, OUT p_name varchar(20),OUT p_address varchar(20), OUT p_subject varchar(20)) -> BEGIN -> SELECT name, address, subject INTO p_name, p_address, p_subject -> FROM student_info -> WHERE id = p_id; -> END // Query OK, 0 rows affected (0.03 sec)
在上面的查詢中,除了1個IN參數外,還有4個OUT參數。現在,按照我們要提供的條件值呼叫該過程,如下所示 −
mysql> DELIMITER ; // mysql> CALL Select_studentinfo(110, @p_name, @p_address, @p_subject); Query OK, 1 row affected (0.06 sec) mysql> Select @p_name AS Name,@p_Address AS Address, @p_subject AS Subject; +--------+------------+-----------+ | Name | Address | Subject | +--------+------------+-----------+ | Rahul | Chandigarh | History | +--------+------------+-----------+ 1 row in set (0.00 sec)
以上是如何建立一個MySQL預存程序,從MySQL表傳回多個值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!