473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

subtraction in access from two tables

i want to subtract value from two different tables one is the inventory table and the second is the sales table. i want the quantity sold from the sale table to be subtracted from the quantity in the inventory table base on the name of the product. which is in both tables
Dec 14 '12 #1
3 5552
NeoPa
32,556 Expert Mod 16PB
I'll start by telling you that, by asking this question, you are indicating a fundamental lack of understanding of your basic issue that will almost certainly lead you into difficulties. Without something to indicate that a sale has already been applied, you have nothing to stop such an update from being repeated inappropriately. In many cases too, it is unnecessary to update data in place when the result can be calculated from the existing data (See Database Normalisation and Table Structures).

Next, as it's also very important, you never want to use string values to identify items. It gets messy and easily and frequently leads to problems where things are similar but not recognised as the same due to hard to notice differences. In short, it's never a good idea.

All that said, the simple SQL structure of such a command is :
Expand|Select|Wrap|Line Numbers
  1. UPDATE [tblInventory] AS [tI]
  2.        INNER JOIN
  3.        [tblSales] AS [tS]
  4.    ON  tI.ProductName = tS.ProductName
  5. SET    tI.Quantity = tI.Quantity - tS.QuantitySold
Dec 14 '12 #2
PreethiGowri
126 64KB
Expand|Select|Wrap|Line Numbers
  1. UPDATE inventory_table 
  2. LEFT JOIN sale_table ON inventory_table.name = sale_table.name
  3. SET inventory_table.quantity = (inventory_table.quantity - sale_table.quantity) 
  4. WHERE inventory.name = pen;
Dec 14 '12 #3
NeoPa
32,556 Expert Mod 16PB
While that's very similar to what is already posted, it differs in two aspects - both of which changes are problems.
  1. The LEFT JOIN is not correct as you would certainly not want to process Inventory records where there were no matching Sales records.
  2. The WHERE clause, if it were required - and I don't believe it is, would need to have the value surrounded in quotes (Quotes (') and Double-Quotes (") - Where and When to use them) to show that it is a string literal and not a variable of any sort.

Any other differences are inconsequential and have no bearing on the fundamental effect of the SQL code (so it's mostly right, even if it has a couple of small errors).
Dec 14 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Job Lot | last post by:
Is there any way to programmatically link access tables using vb.net
5
by: Santiago Ordax Solivellas | last post by:
Hi. We have almost all our tables defined on library lib1 and some on lib2. We have alias defined on lib1 to access tables on lib2 so there is no need to qualify library name. Alias for tables on...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
46
by: Adam Turner via AccessMonster.com | last post by:
If I had a field called "Name" in an Access table "Contact Info", and the field contained VBScript... Function Main(rstFields) Main = rstFields.Item("FirstName").Value End Function 1. How do...
0
by: | last post by:
I am updating MS access tables with data in an xml document. I create two dataset, one for existing data and one for new data. I fill the first dataset with the records from MS Access, the second...
1
by: Jliu6 | last post by:
I have 4 related tables in the Access database and data was sent me in XML files. I need to load data from XML files to my existing Access tables not just import XML files to the database. Any...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
2
by: drek01 | last post by:
hi i am trying to import access tables into my backend database( current database) which is sql server. i created file browser which browse access database from harddrive and places all the tables...
3
by: inepu | last post by:
I have some excel sheets that I would like to manage in access. I've created the access tables and relationships; the excel sheet is only one table and with duplicate data, I'd like to make sure that...
13
by: BrokenMachine | last post by:
Hi there, I'm using Access 2003 and Word 2003 and I'm trying to import data from the word form to multiple access tables in a one to many relationship, but I can't seem to figure it out. I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.