MySQL Stored Procedure with Multiple Select statements From Different Tables

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

    Using Multiple Select Statement in Stored Procedures

    Try with the below query.

        select distinct ProdSubCategory, NULL,NULL
    from Table1
    inner join Table2 on ID_FK= ID_PK
    where Table2.Type=@chvType and Table1.ProdCategory=@chvProdSubCategory
    
    UNION ALL
    
    select NULL, Brand,NULL
    from Table1
    inner join Table2 on ID_FK= ID_PK
    where Table2.Type=@chvType and Table1.ProdCategory=@chvProdSubCategory
    
    UNION ALL
    select NULL, Price,NULL
    from Table1
    inner join Table2 on ID_FK= ID_PK
    where Table2.Type=@chvType and Table1.ProdCategory=@chvProdSubCategor
    
    0 讨论(0)
  • 2021-01-26 05:45
    CREATE PROCEDURE get_data ()
    BEGIN
      SELECT Code, Name, Population, Continent FROM Country
        WHERE Continent = 'Oceania' AND Population < 10000;
      SELECT Code, Name, Population, Continent FROM Country
        WHERE Continent = 'Europe' AND Population < 10000;
      SELECT Code, Name, Population, Continent FROM Country
        WHERE Continent = 'North America' AND Population < 10000;
    END;
    
    0 讨论(0)
  • 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 |
    +----------+
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题