简体   繁体   English

从两个不同的表基于它们各自的日期时间字段获取记录-mysql

[英]get records from two different tables based on their respective datetime fields - mysql

I am trying to fetch data from two different tables orders and order_returns by comparing their respective datetime field. 我想取两个不同表中的数据ordersorder_returns通过比较它们各自的时间字段。

Below is the schema 下面是架构

ORDERS table
    order_id | date_1 | amount
       1      10-10-18    50
       2      11-10-18    100
       3      13-10-18    200

Order returns table

order_return_id | order_id | date_2
     1              3       13-10-18

The output should be 输出应为

order_id  | order_return_id  | amount | date_1  | date_2
  1               NULL           50      10-10-18   NULL
  2               NULL           100     11-10-18   NULL
  3               1              200     13-10-18   13-10-18

I know this could be achieved by doing a left join but I want to display the result in this way. 我知道可以通过左连接来实现,但是我想以这种方式显示结果。 Below is the link 以下是链接

the order_returns data should appear when the date_1 matches date_2

https://ibb.co/kfX18p https://ibb.co/kfX18p

The red part should appear where the arrow points 红色部分应出现在箭头指向的位置

Use left join 使用左联接

select order_id , order_return_id, amount,date_1 ,date_2
from ORDERS left join Orderreturns
on date_1=date_2

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

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