通过比较时间来获取和分组数据

时间:2018-02-23 11:32:14

标签: mysql sql

问题:

我有4张表A,B,C和D.

每个表都有相同的字段:id,date,price。

我使用内部联接在x日期显示每个产品A,B,C和D的价格:

  SELECT
  products.A.date as date,
  products.A.price as A_Price,
  products.B.price as B_Price,
  products.C.price as C_Price,
  products.D.price as D_Price

  FROM products.A

  INNER JOIN products.B ON products.B.date = products.A.date
  INNER JOIN products.C ON products.C.date = products.A.date
  INNER JOIN products.D ON products.D.date = products.A.date;

但是,我还想说明每个产品的价格在x日期1小时后是什么。

例如:

enter image description here

1 个答案:

答案 0 :(得分:0)

SELECT A1.date as ActualDate, A1.price as A_price, A2.price as A_afterOnehr,
B1.price as B_price, B2.price as B_afterOnehr
FROM productA A1 
INNER JOIN productA A2 ON date_add(A1.date, INTERVAL 1 hour) = A2.date
INNER JOIN productB B1 
ON A1.date=B1.date INNER JOIN productB B2
ON  date_add(B1.date, INTERVAL 1 hour) = B2.date   

这是表A,B代表相同......

enter image description here

这是输出

enter image description here