gpt4 book ai didi

MySQL 一个结果来自 2 个表

转载 作者:行者123 更新时间:2023-11-28 23:11:14 27 4
gpt4 key购买 nike

大家好

我有下表:

jobs_active:

| id | date_id    | job_id | result   |
|-------------------------------------|
| 1 | 2017-08-28 | 1 | failed |
|-------------------------------------|
| 2 | 2017-08-28 | 2 | failed |
|_____________________________________|

工作历史:

| 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 天。

我已经尝试过使用 join 和 union,但没有成功。有谁知道这是否可能?

最佳答案

你试过子查询吗?

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

关于MySQL 一个结果来自 2 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45926043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com