简体   繁体   English

从两个不同的表中连续减去两列

[英]Subtract two columns from two different tables successively

I have two tables, twentyBuyer and twentySeller , and in both of the tables, I have multiple columns but the one I want to pull out is totalCost .我有两个表, twentyBuyertwentySeller ,在这两个表中,我有多个列,但我想拉出的一列是totalCost I have been trying to figure out a way to do so that I can subtract the twentyBuyer.totalCost from twentySeller.totalCost successively.我一直试图找出一种方法,以便我可以连续从twentySeller.totalCost .总twentyBuyer.totalCost减去二十个twentyBuyer.totalCost .总twentySeller.totalCost The tables are like this below.表格如下所示。

TABLE twentyBuyer表二十买家

ID, Date,        Name,   TotalCost, Type
1,  "today",     "John", 46,        "ONE"
2,  "yesterday", "Doe",  506,       "ONE"

TABLE twentySeller表二十卖家

ID, Date,        Name,     TotalCost, TYPE
1,  "today",     "Franko", 273,       "ONE"
2,  "yesterday", "Bob",    207,       "ONE"

At first, I tried to pull the information using SELECT statements so I could just do the subtraction very easily doing SELECT twentyBuyer.totalCost - twenty_seller.totalCost FROM twentyBuyer, twenty_seller;起初,我尝试使用 SELECT 语句提取信息,这样我就可以很容易地做减法操作SELECT twentyBuyer.totalCost - twenty_seller.totalCost FROM twentyBuyer, twenty_seller; to which I got我得到的

diff
299
-161
233
-227

I thought that this way was wrong so I Googled some and came across joins.我认为这种方式是错误的,所以我在谷歌上搜索了一些并遇到了连接。 When I started using INNER JOIN , I almost got the right results but there were two more results当我开始使用INNER JOIN ,我几乎得到了正确的结果,但还有两个结果

CODE:代码:

SELECT DISTINCT twenty_seller.totalCost, twentyBuyer.totalCost
FROM twenty_seller
LEFt OUTER JOIN twentyBuyer
ON 1=1;


totalCost | totalCost
207       | 506
273       | 506
207       | 46
273       | 46 

I tried to add UNIQUE to it and nothing seemed to work.我试图向它添加 UNIQUE ,但似乎没有任何效果。 I then switched to views but was having similar problems.然后我切换到视图,但遇到了类似的问题。 I deleted the views code but I was running into similar issues.我删除了视图代码,但遇到了类似的问题。

I want the results to be like我希望结果像

difference
-227
299 

I apologize if this is a trivial question but I just cannot figure out how to do this and I think there is a simple solution that I cannot think of yet.如果这是一个微不足道的问题,我深表歉意,但我无法弄清楚如何做到这一点,我认为有一个我还想不到的简单解决方案。 Any help appreciated任何帮助表示赞赏

The database doesn't attempt to match rows from the two tables - it performs a Cartesian product (ie, matching every row from the first table with every row of the second table), and it's up to you to provide a condition telling the database how to join these rows.数据库不会尝试匹配两个表中的行 - 它执行笛卡尔积(即,将第一个表中的每一行与第二个表中的每一行进行匹配),并且由您提供一个条件告诉数据库如何加入这些行。

In the first example, you don't have a condition, so you're getting 2*2 = 4 rows.在第一个示例中,您没有条件,因此您将获得 2*2 = 4 行。 This is called a cross-join.这称为交叉连接。 In the second example, the condition of 1=1 always evaluates as true, so although it doesn't like it, it's also a cross-join, and produces the same results as the first query.在第二个示例中,条件 1=1 始终评估为 true,因此尽管它不喜欢它,但它也是一个交叉连接,并产生与第一个查询相同的结果。

Assuming you meant to match the rows with the same IDs, you need to provide this logic as a condition:假设您打算匹配具有相同 ID 的行,您需要提供此逻辑作为条件:

SELECT td.id, ts.totalCost - tb.totalCost
FROM   twenty_seller ts
JOIN   twentyBuyer tb ON ts.id = tb.id;

You're looking for the "AS" clause with some math thrown in.您正在寻找带有一些数学运算的“AS”子句。

You're also not giving it any columns to match, which means that ALL the rows from BOTH tables are just going to be returned as is.您也没有为其提供任何要匹配的列,这意味着两个表中的所有行都将按原样返回。

You can fix this by giving it a specific column to match.您可以通过为其提供要匹配的特定列来解决此问题。

Like so:像这样:

SELECT 
  twenty_seller.totalCost AS 'cost1',
  twentyBuyer.totalCost AS 'cost2',
  twenty_seller.totalCost-twentyBuyer.totalCost AS 'Difference'
FROM twenty_seller
LEFT OUTER JOIN twentyBuyer ON twenty_seller.ID=twentyBuyer.ID; 

Or, if you just want the difference:或者,如果您只是想要区别:

SELECT 
  twenty_seller.totalCost-twentyBuyer.totalCost AS 'Difference'
FROM twenty_seller
LEFT OUTER JOIN twentyBuyer ON twenty_seller.ID=twentyBuyer.ID; 

And if you want the total over all the rows:如果你想要所有行的总数:

SELECT 
  SUM(twenty_seller.totalCost)-SUM(twentyBuyer.totalCost) AS 'Difference'
FROM twenty_seller
LEFT OUTER JOIN twentyBuyer ON twenty_seller.ID=twentyBuyer.ID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM