subtracting 2 columns of different tables with a update query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PreethiGowri
    New Member
    • Oct 2012
    • 126

    subtracting 2 columns of different tables with a update query

    I want to subtract 2 column of two different tables and update in one of the table say,
    i have two tables 'main' and 'sub' both tables have a column named 'quantity' so i have to do it in this way:

    update main set quantity = ((quantity of main) - (quantity of sub)) where name = pens;

    Somebody help me please.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    You've solved it right there!

    Build your query linking the two tables on the "name" field (which btw is a reserved word... you should change that) now create a calculated field between the two quantity fields.

    Comment

    • PreethiGowri
      New Member
      • Oct 2012
      • 126

      #3
      i tried doing so,
      Code:
      SELECT main.quantity, sub.quantity
      FROM main
      LEFT JOIN sub
      ON main.name=sub.name;
      Code:
      update main set quantity = (quantity - quantity) where name = pen;
      but this isn't working :(:(
      Last edited by zmbd; Oct 30 '12, 10:46 AM. Reason: please format posted code/sql using the <CODE/> button.

      Comment

      • VanessaMeacham
        New Member
        • Sep 2012
        • 31

        #4
        HI ! you can also try this :
        Code:
        SELECT (SELECT COUNT(quantity) FROM t1) - (SELECT COUNT(quantity) FROM t2)
        Last edited by zmbd; Oct 30 '12, 10:45 AM. Reason: please format posted code/sql using the <CODE/> button.

        Comment

        • PreethiGowri
          New Member
          • Oct 2012
          • 126

          #5
          i don't want to subtract the count of quantity columns but i want to subtract contents of it,
          example -table1 main
          quantity = 5, name = pen
          table2 sub
          quantity = 2, name = pen

          query something like update main set quantity = (main.quantity - sub.quantity) where name = pen;
          to which the answer seem to be

          table main
          quantity = 3, name = pen

          Comment

          • PreethiGowri
            New Member
            • Oct 2012
            • 126

            #6
            Code:
            SELECT ( SELECT quantity FROM main where name = 'pen') - ( SELECT quantity FROM sub where name = 'pen' )
            This query works fine, but is there any way through which i can update this value into main table, in a row where name = pen?
            Last edited by zmbd; Oct 30 '12, 10:45 AM. Reason: please format posted code/sql using the <CODE/> button.

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              PreethiGowri:
              Normally one does not store the result of a calculation unless needed for historical or other such reasons (say a booked room at a special discount booked in the far future).

              Comment

              • PreethiGowri
                New Member
                • Oct 2012
                • 126

                #8
                I agree, but in my case, I need the data to stored, as i'm working on a database regarding shopping,
                as and when the customer purchases some product i should update about the count of how many more are left over:(

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  You actually don't have to store it because you can always calculate the inventory by summing the incoming and subtracting the outgoing. But it can be done both ways. Usually, if you go that route, you would also store the incoming and outgoing in another table so you still have that historical data so you can reconcile the point in time table.

                  But I digress. You've separated your select and update statements, they need to be combined. You also need to handle your nulls since you're using an outer join.

                  Comment

                  • PreethiGowri
                    New Member
                    • Oct 2012
                    • 126

                    #10
                    Code:
                    update main 
                       set quantity = (quantity - quantity) 
                       WHERE 
                          (SELECT 
                             main.quantity, 
                             sub.quantity
                          FROM main
                             LEFT JOIN sub
                                ON main.name=sub.name);
                    i tried this but it shows me an error saying- operand should contain 1 column(s):(
                    Last edited by zmbd; Oct 31 '12, 02:19 PM. Reason: [Z:(Stepped SQL for an easier read.)]

                    Comment

                    • zmbd
                      Recognized Expert Moderator Expert
                      • Mar 2012
                      • 5501

                      #11
                      MySQL has an unusal Update

                      I think MySQL does something wierd in this case.

                      I'm reading thru a PDF file I have that is for a fairly old version of MySQL so the following is most likely not correct; however, if I understand what I'm reading (and I'm not sure that I am) then give this a whirl:
                      Code:
                      UPDATE Main 
                      LEFT JOIN Sub ON main.name = sub.name
                      SET main.quantity = (quantity - quantity);

                      (edit) Oh... I don't have the nulls handled as Rabbit suggested; however, I thought start simple as I'm not sure this works :)(/edit)
                      Last edited by zmbd; Oct 31 '12, 02:35 PM.

                      Comment

                      • PreethiGowri
                        New Member
                        • Oct 2012
                        • 126

                        #12
                        this query is superb:) but i have a problem with this:(
                        it sets the rest of quantity cells as null, as follow
                        ----------+
                        quantity |
                        ----------+
                        NULL |
                        NULL |
                        NULL |
                        NULL |
                        NULL |
                        10 |
                        ----------+

                        Comment

                        • PreethiGowri
                          New Member
                          • Oct 2012
                          • 126

                          #13
                          I solved it :) here is the complete code that works perfectly:)
                          Code:
                          UPDATE Main 
                          LEFT JOIN Sub ON main.name = sub.name
                          SET main.quantity = (main.quantity - sub.quantity) 
                          WHERE main.name = pen;
                          Thank you rabbit
                          Thank you Zmbd
                          Thanks a ton for your support:):)
                          Last edited by zmbd; Nov 1 '12, 12:41 PM. Reason: [Z(Moved the WHERE so that it could be seen w/o scroll)]

                          Comment

                          • PreethiGowri
                            New Member
                            • Oct 2012
                            • 126

                            #14
                            VanessaMeacham,
                            Forgot to thank you, thank you so much dude

                            Comment

                            • zmbd
                              Recognized Expert Moderator Expert
                              • Mar 2012
                              • 5501

                              #15
                              Ahh... I knew I had fogotten something in that SQL!

                              Comment

                              Working...