大家好,
我有以下表格:
jobs_active:
| id | date_id | job_id | result |
|-------------------------------------|
| 1 | 2017-08-28 | 1 | failed |
|-------------------------------------|
| 2 | 2017-08-28 | 2 | failed |
|_____________________________________|
jobs_history:
| id | job_id | date_id | job_id | result |
|----------------------------------------------|
| 1 | 1 | 2017-08-27 | 1 | failed |
|----------------------------------------------|
| 2 | 1 | 2017-08-26 | 1 | success |
|----------------------------------------------|
| 3 | 2 | 2017-08-27 | 2 | failed |
|----------------------------------------------|
| 4 | 2 | 2017-08-26 | 2 | failed |
|______________________________________________|
我想得到这个结果:
(2017-08-28)| (2017-08-27) | (2017-08-26)
| id | date_id | job_id | result_now | result_lastDay1 | result_lastDay2 |
|----------------------------------------------------------------------------|
| 1 | 2017-08-27 | 1 | failed | failed | success |
|----------------------------------------------------------------------------|
| 2 | 2017-08-26 | 2 | failed | failed | failed |
|____________________________________________________________________________|
“result_lastDayN”列应该是动态的。因此,如果需要,我可以选择最后10天。
我已经尝试过加入和联盟,但我没有让它发挥作用。 有没有人知道这是否可能?
答案 0 :(得分:1)
你试过子查询吗?
select ja.*,
(select result from jobs_history jh where job_id = ja.id and jh.date = j.date - INTERVAL 1 DAY) result_lastDay1,
(select result from jobs_history jh where job_id = ja.id and jh.date = j.date - INTERVAL 2 DAY) result_lastDay2
from
jobs_active ja