我有两个不共享主键的表(A和B)。我需要某种连接,其中如果date_B
*日期格式:yyyy-mm-dd
表A
+-------+-----+-----------+
| id_A | op_A| date_A |
+-------+-----+-----------+
| 1 | 0 | 2018-10-01|
| 2 | 0 | 2018-12-31|
+-------+-----+-----------+
表B
+-----+--------+-------------+
| id_b| other | date_B |
+-----+--------+-------------+
| a | 8 | 2018-09-01 |
| b | 90 | 2018-12-01 |
+-----+--------+-------------+
正确的结果:
+-----+-----+-----------+-----+--------+-------------+
| id_A| op_A| date_A | id_B| other | date_B |
+-----+-----+-----------+-----+--------+-------------+
| 1 | 0 | 2018-10-01| a | 8 | 2018-09-01 |
| 2 | 0 | 2018-12-31| a | 8 | 2018-09-01 |
| 2 | 0 | 2018-12-31| b | 90 | 2018-12-01 |
+-----+-----+-----------+-----+--------+-------------+
答案 0 :(得分:3)
此查询应为您提供所需的结果:
SELECT *
FROM TableA a
JOIN TableB b ON b.date_B < a.date_A
输出:
id_A op_A date_A id_b other date_B
1 0 2018-10-01 a 8 2018-09-01
2 0 2018-12-31 a 8 2018-09-01
2 0 2018-12-31 b 90 2018-12-01