简体   繁体   English

如何从一个表返回所有行并从另一个表返回值(如果不存在则返回0)?

[英]How to return all rows From one table and return value from another table, if not present return 0?

I have two tables in my database: 我的数据库中有两个表:

Table1: Purchased Item 表1:采购物品

Item | Qnt | Rate |Total
Meat | 1   | 20   | 20
Fish | 0.5 | 30   | 15
Chicken|1  | 25   | 25
Meat | 2   | 20   | 40


Table2: Trading Price
Item |      Datetime       | Price
Meat | 2013-02-20 10:00:00 | 20
Meat | 2013-02-20 09:00:00 | 18
Meat | 2013-02-19 08:00:00 | 21
Fish | 2013-02-19 09:00:00 | 15
Fish | 2013-02-19 08:00:00 | 17
Chicken|2013-02-20 09:00:00|26
Chicken|2013-02-20 08:00:00|25

The Table1 lists purchased Item and Table2 updates in every hour with the current price of each Item. 表1列出了每小时购买的商品,而Table2则以每个商品的当前价格每小时更新一次。 So from Table2, it is clearly seen that Meat was last traded on 20-02-2013 at 10 AM Whereas the Fish was not traded on the same day, it was traded on 19-02-2013 at 9AM and the Chicken was traded on 20-02-2013 at 9AM. 因此,从表2中可以清楚地看到,肉类的最后交易时间是2013年2月20日上午10点,而鱼不是在同一天进行交易,鱼的交易时间是2013年2月19日上午9点,而鸡肉的交易时间是2013年2月20日上午9点。 What I want to do, list all items from table 1 and join the last trade price of respective items from table 2 which will like this: 我想做的是,列出表1中的所有项目,并加入表2中各个项目的最后交易价格,如下所示:

Output:
Item | Qty | Total | Last Trade Price
Meat | 1   | 20    | 20
Meat | 2   | 40    | 20
Fish | 0.5 |15     | 15
Chicken|1  |25     |26

What type of join and what clause should applied here to get the desirable output? 为了获得理想的输出,应在此处应用哪种类型的联接和什么子句? I tried with this query: SELECT p.Item, p.Qnt l.price FROM Table1 as p INNER JOIN Table2 as l ON p.Item=l.Item WHERE l.Datetime=(SELECT max(Datetime) FROM Table2); 我尝试使用此查询:SELECT p.Item,p.Qnt l.price FROM Table1 as p INNER JOIN Table2 as l ON p.Item = l.Item WHERE l.Datetime =(SELECT FROM Table2的max(Datetime)); But by this query I am not finding the desired result as max Datetime is not same for each item, that's why item is missing from output. 但是通过此查询,我找不到期望的结果,因为每个项目的最大日期时间都不相同,这就是为什么输出中缺少项目。

So what join type and what condition should be applied in WHERE Clause to get the above Output? 那么在WHERE子句中应该应用什么连接类型和什么条件才能获得上面的输出?

Try this query: 试试这个查询:

SELECT p.Item, p.Qnt, LatestPriceTable.price FROM Table1 as p 

INNER JOIN

(SELECT Table2.Item, Table2.Price FROM Table2 INNER JOIN
(SELECT Item, MAX(DateTime) FROM Table2
GROUP BY Item) AS A ON Table2.Item = A.Item) AS LatestPriceTable

ON p.Item=LatestPriceTable.Item

The inner most query SELECT Item, MAX(DateTime) FROM Table2 GROUP BY Item will ouput that lastest DateTime for each Item . 最里面的查询SELECT Item, MAX(DateTime) FROM Table2 GROUP BY Item将输出每个Item最新DateTime We call this result A. Now we join A with Table2 to get the Price column value on those lastest dates. 我们将此结果称为A。现在,我们将A与Table2结合起来,以获取最近日期的Price列值。 The resulting table, called LatestPriceTable, will only contain the Item name and the latest price. 结果表称为LatestPriceTable,将仅包含商品名称和最新价格。 Now you can simply join it with Table1 to do your computations. 现在,您可以简单地将它与Table1结合起来进行计算。

I do have a feeling that the system you have designed may not be good enough for what you're trying to achieve. 我确实感觉到您设计的系统可能无法满足您想要实现的目标。 Is it an inventory kind of application? 它是库存类型的应用程序吗? The problem I see is that you are always multiplying the current stock quantity with the latest market price. 我看到的问题是,您总是将当前的库存数量乘以最新的市场价格。 In reality, the stock will almost always contain purchases made on different days and with different rates. 实际上,库存几乎总是包含在不同日期和不同利率下进行的购买。

But then again, I'm not sure about your exact purpose, so maybe you could just live with what you're doing. 但是话又说回来,我不确定您的确切目的,所以也许您可以继续自己的工作。

暂无
暂无

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

相关问题 返回表中的所有行以及其他表中特定行的信息 - Return all rows from a table and information from another for specific row MySQL:如何返回表中的所有行,并从另一个表中计算具有匹配ID的行数 - MySQL: How to return all rows in a table and count the amount of rows with matching ID from another table 使用JOIN和WHERE返回一个表中的所有行,而返回另一个表中的仅少数行 - Return all rows from one table and only a few from another with JOIN and WHERE 如何返回一个表中其字段包含来自另一表中任何行的字符串的行? - How to return rows in one table whose field contains a string from any row in another table? 如何选择联接并仅返回一个表中的行? - How to select with joins and only return rows from one table? 尝试返回一个表中的所有行并计算另一个表中具有匹配ID的行数 - try to return all rows in a table and count the amount of rows with matching ID from another table 连接两个表以返回一个表中的所有行及其在另一个表中的状态 - Join two tables to return all rows in one and their status in another table 返回表中的所有字段或返回一个查询中不存在的字段 - Return all fields from table or return that not exist in one query 如何根据另外两个表的过滤器快速返回和计算一个表中的行数 - How to fast return and count rows from one table based on filter by two another tables SQL从父表返回条件与子表的另一个子表匹配的所有行 - Sql return all rows from parent table where criteria match from another child of a child table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM