title: MySQL Stored Procedure with Multiple Tables
body: <p>I want to show all the records of my <code>items</code> table. But some quantity need to be a negative value.</p>
  
  <pre><code>BEGIN 
      SET @ItemId1 = (SELECT ItemId FROM items);
  
      SELECT 
          SUM(ROUND(Quantity)) 
      FROM (
          SELECT
              COALESCE(SUM(od.Quantity),0) as Quantity
          FROM sts as T1
          LEFT JOIN orderset_details as od 
              ON T1.OrdersetId = od.OrdersetId
          WHERE od.ItemId = @ItemId1
          UNION ALL
          SELECT 
              COALESCE(SUM(od.Quantity),0) as Quantity
          FROM drs as T2
          LEFT JOIN orderset_details as od 
              ON T2.OrdersetId = od.OrdersetId
          WHERE od.ItemId = @ItemId1
          UNION ALL  
          SELECT 
              COALESCE(
                  SUM(
                      IF(r.AddsToStock = '1', od.Quantity, 0 - od.Quantity)
                  ),0
              ) as Quantity
          FROM stockadjustment as T3
          LEFT JOIN reason as r 
              ON r.ReasonId = T3.ReasonId
          LEFT JOIN orderset_details as od 
              ON T3.OrdersetId = od.OrdersetId 
          WHERE od.ItemId = @ItemId1
          UNION ALL
          SELECT 
              COALESCE(
                  SUM(
                      IF(T4.OrdersetId = od.OrdersetId, 0 - od.Quantity, -1 * od.Quantity)
                  ),0
              ) as Quantity
          FROM issueslip as T4
          LEFT JOIN orderset_details as od 
              ON T4.OrdersetId = od.OrdersetId 
          WHERE od.ItemId = @ItemId1
          UNION ALL
          SELECT 
              COALESCE(
                  SUM(
                      IF(T4.OrdersetId = od.OrdersetId, 0 - od.Quantity, -1 * od.Quantity)
                  ),0
              ) as Quantity
          FROM invoice as T4
          LEFT JOIN orderset_details as od 
              ON T4.OrdersetId = od.OrdersetId
          WHERE od.ItemId = @ItemId1
      ) as t;
  END
  </code></pre>
  
  <p>This code got an error <code>MySQL said: #1242 -Subquery returns more than 1 row</code>. </p>
  
  <p>I want all the quantity of every item to be shown. Thats why I have @ItemId1 to get all the <code>ItemId from items</code>. How Can I do that?</p>