I\'m trying to do multiple selects from different tables in a mysql stored procedure as follows
DELIMITER //
CREATE PROCEDURE `NovemberSummary`(IN `branch` VA
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
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;
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 |
+----------+
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