MYSQL:如何获取多表的当前条目

时间:2016-03-27 13:14:27

标签: mysql

我有三张包含产品数据的表格: 定义产品的地方。 第二个定义哪个商店的产品具有时间戳的价格。 还有三分之一的产品存放在被分配的产品中。

Table 1 - products:
prdoductID|Name
----------|-------
1         |banana
2         |apple

Table 2 - prices:
storeID | productID | price | timestamp
--------|-----------|-------|------------
1       | 1         | 5,90  | 2016-03-27 16:00:00
1       | 1         | 5,90  | 2016-03-27 17:00:00
2       | 1         | 5,00  | 2016-03-27 16:00:00
2       | 2         | 5,00  | 2016-03-27 16:00:00
2       | 2         | 5,90  | 2016-03-28 19:00:00

Table 3 - stocks:
storeID | productID | stock | timestamp
--------|-----------|-------|------------
1       | 1         | 50    | 2016-03-27 08:00:00
1       | 1         | 5     | 2016-03-27 17:00:00
2       | 1         | 60    | 2016-03-27 09:00:00
2       | 2         | 0     | 2016-03-27 16:00:00
2       | 2         | 55    | 2016-03-28 19:00:00

` 现在我想获得一家商店的当前状态,例如2016-03-27 14:00:00。

我当前的查询为我提供数据,但是对于所有时间戳。我只想用storeID = x获得该商店每个产品的最新时间戳的相应推文;

注意:用户可以进行计划。因此可能存在具有未来时间戳的条目,因此只有MAX不起作用。

Query:
SELECT price.*, prod.name, stock.timestamp as 'stockTimesamp',
stock.stock FROM prices price 
LEFT OUTER JOIN products prod on price.productID = prod.productID and 
price.timestamp=(SELECT MAX(price.timestamp) WHERE price.timestamp <= '2016-03-27 14:00:00' and price.productID = prod.productID) 
LEFT OUTER JOIN stocks stock on price.storeID = best.storeID and price.productID = stock.productID and stock.timestamp=(SELECT MAX(best.timestamp) WHERE stock.timestamp <= '2016-03-27 14:00:00' and stock.productID = prod.productID and stock.productID = price.productID) 
where price.storeID=21
ORDER BY stock.timestamp DESC

希望你能帮助我...

1 个答案:

答案 0 :(得分:0)

对于此类查询,这是一个麻烦的数据结构。您可以获取每个表的每个产品的最新时间戳,然后加入以获取其他信息:

select p.*,
       (select max(timestamp)
        from stocks s
        where s.storeid = $storeid and s.productid = p.productid and
              s.timestamp <=  '2016-03-27 14:00:00'
      ) as stock_timestamp,
       (select max(timestamp)
        from prices pr
        where pr.storeid = $storeid and pr.productid = p.productid and
              pr.timestamp <=  '2016-03-27 14:00:00'
      ) as stock_timestamp     
from products p;

然后,加入回原始表格以获取更多信息:

select p.*, s.*, pr.*
from (select p.*,
             (select max(timestamp)
              from stocks s
              where s.storeid = $storeid and s.productid = p.productid and
                    s.timestamp <=  '2016-03-27 14:00:00'
            ) as stock_timestamp,
             (select max(timestamp)
              from prices pr
              where pr.storeid = $storeid and pr.productid = p.productid and
                    pr.timestamp <=  '2016-03-27 14:00:00'
            ) as price_timestamp     
      from products p
     ) p left join
     stocks s
     on s.storeid = $storeid and s.productid = p.productid and
        s.timestamp = p.stock_timestamp left join
     prices pr
     on pr.storeid = $storeid and pr.productid = p.productid and
        pr.timestamp = p.price_timestamp ;

我应该注意,如果您在每条记录上都有一个有效的结束日期,那么您的问题将会相对微不足道。这是存储缓慢变化维度的更好方法。