MySQL Stored Procedure with Multiple Select statements From Different Tables

后端 未结 4 517
萌比男神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:57

    Try This

    CREATE PROCEDURE `NovemberSummary`(IN `branch` VARCHAR(60), IN `year` INT) NOT 
    
    DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER 
    
    BEGIN
    
    DECLARE SalesAmount VARCHAR(255) DEFAULT 0;
    DECLARE ExpensesAmount VARCHAR(255) DEFAULT 0;
    
    SELECT SUM(sales.amount) AS Sales INTO SalesAmount FROM sales WHERE MONTH (sales.date)= 11 AND 
    
    sales.branch = branch;
    
    SELECT SUM(expenses.amount) AS Expenses INTO ExpensesAmount FROM expenses WHERE MONTH(expenses.date)= 11
    
    AND expenses.branch = branch;
    
    SELECT SalesAmount AS Sales, ExpensesAmount AS Expenses;
    
    END
    

提交回复
热议问题