一个[MYSQL]一个结果中的两个查询

时间:2017-09-29 10:44:56

标签: mysql join

你可以帮助我们加入两个查询吗? 这是我的第一个问题:

SELECT estimated_sum_all, story_point_sum_all, time_spent,  reg_date
FROM burndown_snap_all  WHERE project_id='72'

结果:

 | estimated_sum_all | story_point_sum_all | time_spent | reg_date |
 | 300               | 20                  | 20.30      | 2017-09  |
 | 300               | 20                  | 19.30      | 2017-09  |
 | 300               | 20                  | 18.30      | 2017-09  |
 | 300               | 20                  | 15.32      | 2017-09  |

这是我的第二个问题:

SELECT time_spent FROM status_history_hours where
project_id = '72'

结果:

| time_spent | 
| 20.30      |
| 20.30      |
| 20.30      |
| 20.30      |

我想要做的是构建一个mysql查询,该查询必须包含来自第二个查询的select / join到time_spent。决赛桌应如下所示:

 | estimated_sum_all | story_point_sum_all | time_spent | reg_date |
 | 300               | 20                  | 20.30      | 2017-09  |
 | 300               | 20                  | 20.30      | 2017-09  |
 | 300               | 20                  | 20.30      | 2017-09  |
 | 300               | 20                  | 20.30      | 2017-09  |

此致

解决方案就像:

SELECT t1.id, t1.estimated_sum_all, 
t1.story_point_sum_all, t1.time_spent, 
t2.id, t2.time_spent FROM 
burndown_snap_all t1, status_history_hours 
t2 WHERE t1.project_id = 72 AND 
t2.project_id = 72 group by t1.id 

但是如何在同一时间按t2.id分组???

2 个答案:

答案 0 :(得分:2)

SELECT estimated_sum_all, story_point_sum_all, time_spent,  reg_date
FROM burndown_snap_all  WHERE project_id='72'
UNION ALL
SELECT time_spent FROM status_history_hours where
project_id = '72'

答案 1 :(得分:1)

根据您提供的数据,您可以从第二个表中获取任何匹配值。所以,你可以这样做:

SELECT bsa.estimated_sum_all, bsa.story_point_sum_all, bsa.time_spent,
       bsa.reg_date,
       (SELECT MAX(shh.time_spent)
        FROM status_history_hours shh
        WHERE shh.project_id = bsa.project_id
       ) as time_spent
FROM burndown_snap_all bsa
WHERE project_id = 72;