SQL Simple Subtraction Query

怎甘沉沦 提交于 2019-12-10 21:57:04

问题


I have a Database. It contains 2 Tables. Let me call them Table A and Table B.

Table A Content:

  1. Date
  2. Name of Supplier
  3. Name of Good Delivered
  4. Amount of Good Delivered, simply put Ingrediënt A (Numeric Value)

Table B Content:

  1. Production Date
  2. Name of Product
  3. Ingrediënt A

What I exactly want is:

Query with Table A, Column 4 minus (Subtraction) Table B, Column 3.

Extra Notes:

I have been asking this question to a friendly programmer. He adviced me to go through the onlince course of w3schools.com which I did. He also assumed the subtraction should belong under a certain 'Join' function.

I have also been googling and searching Stackoverflow. With no results. Even tho, I need to mention that I maybe lack finding the results because my English is quite poor.

I want to sincerely apologise, if my English is poor and if my question was / is poorly phrased. English is not my native language.

If direct help is not possible, I am willing and eager to learn. If you have any sources where I can learn about simple mathematics in Querys related to Databases (Such as MS Access or OOo Base) than feel free to refer me to good and informational websites and / or courses.

Sincerely yours and with kind regards Dofty

System Information:

  • Windows 7 - 64 Bit - Home Edition and all it's updates
  • OpenOffice.org 3.3.0 - OOO330m20(Build: 9567)

Reason for not upgrading OOo to latest version: Compatibility Issues with previous scripted software. And too 'less' time at the job to fix those issues for Version 4.0 and later.


回答1:


You can just use math in SQL

SELECT 
    (a.column_4 - b.column_3) as subtracted_value 
FROM 
    TABLE_A as a 
JOIN 
    TABLE_B as b ON a.id = b.table_a_id

"subtracted_value" should be the value of column 4 of table a minus column 3 of table b here. You need to join the tables using an id so you can see which row of TABLE A belongs to which row of TABLE B.

In this example I called the id in TABLE A "id" and the pointer in TABLE B to the id of TABLE A "table_a_id". So "id" in TABLE A and "table_a_id" in TABLE B need to be equal when they belong to the same row.

Worth taking a look at: http://www.w3schools.com/sql/sql_join.asp




回答2:


are you trying to do this with a SQL query? How about something based on: SELECT (SELECT COLUMN4 FROM tableA) - (SELECT COLUMN3 FROM tableB)



来源:https://stackoverflow.com/questions/23754596/sql-simple-subtraction-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!