MySQL Stored Procedure with Multiple Select statements From Different Tables

后端 未结 4 513
萌比男神i
萌比男神i 2021-01-26 05:33

I\'m trying to do multiple selects from different tables in a mysql stored procedure as follows

DELIMITER //  
CREATE PROCEDURE `NovemberSummary`(IN `branch` VA         


        
4条回答
  •  孤城傲影
    2021-01-26 05:52

    Try this approach:

    DELIMITER //  
    CREATE PROCEDURE `NovemberSummary`(IN `branch` VARCHAR(60), IN `year` INT) NOT 
    DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER 
    BEGIN
    SELECT 
       ( select sum(sales.amount) from sales 
         where month (sales.date)= 11 and sales.branch = branch ) as Sales ,
       ( select sum(expenses.amount) from expenses 
         where month(expenses.date)= 11 and expenses.branch = branch ) as Expenses 
       ;
    END
    

    this procedure returns only one resultset that contains two columns: Sales + Expenses:

    +-------+----------+
    | Sales | Expenses |
    +-------+----------+
    |    20 |       15 |
    +-------+----------+
    

    , instead of two resultsets with only one column.

    +-------+
    | Sales |
    +-------+
    |    20 |
    +-------+
    
    +----------+
    | Expenses |
    +----------+
    |       15 |
    +----------+
    

提交回复
热议问题