SQL SQL简单减法查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23754596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Simple Subtraction Query
提问by Brecht De Baere
I have a Database. It contains 2 Tables. Let me call them Table A and Table B.
我有一个数据库。它包含2个表。让我称它们为表 A 和表 B。
Table A Content:
表A内容:
- Date
- Name of Supplier
- Name of Good Delivered
- Amount of Good Delivered, simply put Ingredi?nt A (Numeric Value)
- 日期
- 供应商名称
- 货物名称
- 已交付货物的数量,只需输入成分 A(数值)
Table B Content:
表B内容:
- Production Date
- Name of Product
- Ingredi?nt A
- 生产日期
- 产品名称
- 成分 A
What I exactly want is:
我真正想要的是:
Query with Table A, Column 4 minus (Subtraction) Table B, Column 3.
查询表 A,第 4 列减去(减法)表 B,第 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.
我一直在向一位友好的程序员提出这个问题。他建议我完成 w3schools.com 的在线课程,我也是这样做的。他还假设减法应该属于某个“加入”函数。
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.
我也一直在谷歌搜索和搜索 Stackoverflow。没有结果。即便如此,我还是要提一下,我可能找不到结果,因为我的英语很差。
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.
如果无法直接提供帮助,我愿意并渴望学习。如果您有任何资源可以让我在与数据库相关的查询(例如 MS Access 或 OOo Base)中学习简单的数学知识,请随时向我推荐优秀的信息网站和/或课程。
Sincerely yours and with kind regards Dofty
真诚地向您致以亲切的问候 Dofty
System Information:
系统信息:
- Windows 7 - 64 Bit - Home Edition and all it's updates
- OpenOffice.org 3.3.0 - OOO330m20(Build: 9567)
- Windows 7 - 64 位 - 家庭版及其所有更新
- OpenOffice.org 3.3.0 - OOO330m20(内部版本: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.
未将 OOo 升级到最新版本的原因:与以前的脚本软件的兼容性问题。并且在工作中修复 4.0 及更高版本的这些问题的时间太“少”。
回答by Sinan Samet
You can just use math in SQL
你可以在 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.
“subtracted_value”应该是表 a 第 4 列的值减去表 b 的第 3 列。您需要使用 id 连接表,以便您可以查看 TABLE A 的哪一行属于 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.
在这个例子中,我将 TABLE A 中的 id 称为“id”,将 TABLE B 中的指针称为指向 TABLE A 的 id 的指针“table_a_id”。因此,表 A 中的“id”和表 B 中的“table_a_id”在属于同一行时需要相等。
Worth taking a look at: http://www.w3schools.com/sql/sql_join.asp
回答by RobP
are you trying to do this with a SQL query? How about something based on: SELECT (SELECT COLUMN4 FROM tableA) - (SELECT COLUMN3 FROM tableB)
你想用 SQL 查询来做到这一点吗?怎么样基于: SELECT (SELECT COLUMN4 FROM tableA) - (SELECT COLUMN3 FROM tableB)